我有一个 Web 应用程序进行身份验证,然后检查请求中的 cookie 以允许来自同一客户端的后续调用。我写了一段代码,当我从 Eclipse 运行它时工作得很好,但从 android 模拟器运行时失败。这是我所做的
为 URI 创建 web 资源并将过滤器绑定到它以将 cookie 附加到请求的类
public class TolvenRestClient {
private static Client client;
private static final ThreadLocal<String> instance = new ThreadLocal<String>();
static {
ClientConfig config = new DefaultClientConfig();
client = Client.create(config);
client.setFollowRedirects(true);
}
private Client getClient() {
return client;
}
public WebResource getResource(String path){
WebResource webResource = getClient().resource(path);
webResource.addFilter(new ClientFilter() {
@Override
public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {
if (instance.get() != null) {
clientRequest.getHeaders().putSingle("Cookie", "SSOCookie=" + instance.get());
}
ClientResponse clientResponse = getNext().handle(clientRequest);
String cookieName = "SSOCookie";
System.out.println("cookies "+clientResponse.getCookies());
for (NewCookie newCookie : clientResponse.getCookies()) {
if (cookieName.equals(newCookie.getName())) {
String extendedSessionId = newCookie.getValue();
instance.set(extendedSessionId);
break;
}
}
return clientResponse;
}
});
return webResource;
}
然后是触发 web 服务调用的示例代码
RestClient client = new RestClient();
MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
formData.add("username", "demo");
formData.add("password", "1234qwer");
formData.add("realm", "tolven");
WebResource webResource = client.getResource("https://dev.example.com:8443/gatekeeper/rs/authenticate/login");
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,formData);
//ClientResponse response = webResource.accept("text/html").get(ClientResponse.class);
System.out.println("Here ....." + response.toString());
webResource = client.getResource("https://dev.example.com:8443/api/vestibule/accountList");
response = webResource.get(ClientResponse.class);
Accounts accounts = response.getEntity(Accounts.class);
从 Eclipse 运行时,此代码运行良好。但是当在 android 模拟器中运行时,它无法从客户端响应中读取 cookie
我得到以下异常
09-20 19:55:31.067: W/System.err(1172): java.lang.NullPointerException
09-20 19:55:31.077: W/System.err(1172): at javax.ws.rs.core.NewCookie.valueOf(NewCookie.java:126)
09-20 19:55:31.077: W/System.err(1172): at com.sun.jersey.api.client.ClientResponse.getCookies(ClientResponse.java:714)
09-20 19:55:31.077: W/System.err(1172): at com.userexample.LoginActivityTolven$1$1.handle(LoginActivityTolven.java:71)
09-20 19:55:31.088: W/System.err(1172): at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
09-20 19:55:31.088: W/System.err(1172): at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
09-20 19:55:31.097: W/System.err(1172): at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
09-20 19:55:31.097: W/System.err(1172): at com.userexample.LoginActivityTolven$1.onClick(LoginActivityTolven.java:86)
09-20 19:55:31.097: W/System.err(1172): at android.view.View.performClick(View.java:4240)
09-20 19:55:31.097: W/System.err(1172): at android.view.View$PerformClick.run(View.java:17721)
09-20 19:55:31.107: W/System.err(1172): at android.os.Handler.handleCallback(Handler.java:730)
09-20 19:55:31.107: W/System.err(1172): at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 19:55:31.107: W/System.err(1172): at android.os.Looper.loop(Looper.java:137)
09-20 19:55:31.107: W/System.err(1172): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-20 19:55:31.107: W/System.err(1172): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 19:55:31.117: W/System.err(1172): at java.lang.reflect.Method.invoke(Method.java:525)
09-20 19:55:31.117: W/System.err(1172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-20 19:55:31.117: W/System.err(1172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-20 19:55:31.117: W/System.err(1172): at dalvik.system.NativeStart.main(Native Method)
有没有人尝试过在 android 上使用 Jersey 客户端并看到这个问题?