如何在 Retrofit 中检索 cookie? [英] How to retrieve cookies in Retrofit?
问题描述
我阅读了关于请求拦截器的内容,但不知道如何真正使用它们来获取 cookie...我正在从 nodejs 发送 cookie...
I read about request interceptors and what not but no clue how to really use them to obtain cookies... I am sending the cookie like so from nodejs...
res.cookie('userid', user._id, { maxAge: 86400000, signed: true, path: '/' });
在我的 android 客户端 - 到目前为止我已经为我的 RestApiManager 设置了这个
And in my android client - I have this set up so far for my RestApiManager
public class RestApiManager {
private static final String API_URL = "ip: port";
private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
//Call interface
public interface AsynchronousApi {
//Login User
@FormUrlEncoded
@POST("/login")
public void loginUser(
@Field("loginName") String loginName,
@Field("password") String password,
Callback<UserResponse> callback);
//Profile Picture
@Multipart
@POST("/profilePicture")
public void uploadProfilePicture(
@Part("photo") TypedFile photo,
@Part("userId") String userId,
Callback<UserResponse> callback); //success thumbnail to picasso
}
//create adapter
private static final AsynchronousApi ASYNCHRONOUS_API = REST_ADAPTER.create(AsynchronousApi.class);
//call service to initiate
public static AsynchronousApi getAsyncApi() {
return ASYNCHRONOUS_API;
}
}
单独的 cookie 类:
Separate cookie class:
public class ApiCookie implements RequestInterceptor{
// cookie use
private String sessionId;
public ApiCookie() {
}
//COOKIE BELOW
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public void clearSessionId() {
sessionId = null;
}
@Override
public void intercept(RequestFacade requestFacade) {
setSessionId();
}
}
试图弄清楚如何获取 cookie 并能够在以后的请求中发送它,所以我不需要包含 userId 字段?
trying to figure out how to obtain the cookie and be able to send it with future requests, so I do not need to include a userId field?
推荐答案
我在我的应用中遇到了类似的情况.此解决方案适用于我使用 Retrofit 检索 cookie.MyCookieManager.java
I had similar situation in my app. This solution works for me to retrieve cookies using Retrofit. MyCookieManager.java
import java.io.IOException;
import java.net.CookieManager;
import java.net.URI;
import java.util.List;
import java.util.Map;
class MyCookieManager extends CookieManager {
@Override
public void put(URI uri, Map<String, List<String>> stringListMap) throws IOException {
super.put(uri, stringListMap);
if (stringListMap != null && stringListMap.get("Set-Cookie") != null)
for (String string : stringListMap.get("Set-Cookie")) {
if (string.contains("userid")) {
//save your cookie here
}
}
}
}
以下是如何使用 RequestInterceptor 为将来的请求设置 cookie:
Here is how to set your cookie for future requests using RequestInterceptor:
MyCookieManager myCookieManager = new MyCookieManager();
CookieHandler.setDefault(myCookieManager);
private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade requestFacade) {
String userId = ;//get your saved userid here
if (userId != null)
requestFacade.addHeader("Cookie", userId);
}
})
.build();
这篇关于如何在 Retrofit 中检索 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!