我正在使用网络服务电话处理应用。
我正在调用我的登录网络服务,如下所示
String url = "http://mydomaim.com/login.php";
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(userEmail, password, url);
工作正常,并按照以下显示向我发送回复
{
"userName":"a",
"login_success":1,
"user_id":"3",
"session_id":"1067749aae85b0e6c5c5e697b61cd89d",
"email":"a"
}
我解析了这个响应,并成功地获得了变量中的会话ID。 现在我必须调用另一个webservice,将此session_id附加为cookie值。
问题
如何在我的Android设备中将session_id
存储为我的Cookie值并调用其他网络服务?
答案 0 :(得分:2)
尝试此操作,登录时请拨打此请求。
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (Cookie cookie : cookies) {
System.out.println("Cookie: " + cookie.toString());
if (cookie.getName().contains("PHPSESSID"))
guid = cookie.getValue();
}
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
答案 1 :(得分:1)
您可以尝试这样的事情:
public static void myMethod(Map<String, String> cookies) throws IOException, PortalException {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 60000);
HttpConnectionParams.setSoTimeout(httpParams, 240000);
try {
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, httpParams);
if (cookies != null) {
for (Map.Entry<String, String> entry : cookies.entrySet()) {
BasicClientCookie cookie = new BasicClientCookie(entry.getKey(), entry.getValue());
cookie.setPath("/");
cookie.setDomain(new URL(url).getHost());
httpClient.getCookieStore().addCookie(cookie);
}
}
HttpRequestBase request = null;
if (post) {
request = post(url, params, headers);
} else {
request = get(url, params, headers);
}
BasicHttpContext context = new BasicHttpContext();
HttpResponse response = httpClient.execute(request, context);
//... etc
它对我有用。