I am working on a app using web services calls.
I am calling my Login webservice as follows
String url = "http://mydomaim.com/login.php";
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(userEmail, password, url);
It works fine and send me response as show below
{
"userName":"a",
"login_success":1,
"user_id":"3",
"session_id":"1067749aae85b0e6c5c5e697b61cd89d",
"email":"a"
}
I parse this response, and successfully and got the session id in a variable.
Now I have to call an other webservice appending this session_id as cookie value.
QUESTION
How to store the session_id
as the my cookie value in my Android device and call other web service?
Try this, When login Call this request.
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();
You can try something like this:
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
It works for me.