I am developing an Android app for a website. I'm trying to get some information from the website by sending a cookie using a HttpGet request. I don't know where I am going wrong with the request. I am using AsyncTask to send the request to the server.
The request also uses a token which I receive when I login to the webpage. All this is clear in the code below. This is part of the process in the doInBackground() function of the Asynctask:
BufferedReader in = null;
JSONObject token = Login.getToken(); // Token received after login, used in the cookie
try {
r = token.get("session_token").toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultHttpClient httpclient = (DefaultHttpClient) getHttpClient();
BasicClientCookie ck = new BasicClientCookie("session_token", r);
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
cookieStore.addCookie(ck);
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://example.com/api/page");
try {
HttpResponse response = httpclient.execute(httpget, localContext);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line=null;
for (line = null; (line = in.readLine()) != null
{
builder.append(line).append("\n");
}
The getHttpClient function looks like:
public static final int HTTP_TIMEOUT = 30 * 1000;
private HttpClient mHttpClient;
private HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
If somebody could please help me out. It would be great.
The request also uses a token which I receive when I login to the webpage. All this is clear in the code below. This is part of the process in the doInBackground() function of the Asynctask:
BufferedReader in = null;
JSONObject token = Login.getToken(); // Token received after login, used in the cookie
try {
r = token.get("session_token").toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultHttpClient httpclient = (DefaultHttpClient) getHttpClient();
BasicClientCookie ck = new BasicClientCookie("session_token", r);
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
cookieStore.addCookie(ck);
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://example.com/api/page");
try {
HttpResponse response = httpclient.execute(httpget, localContext);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line=null;
for (line = null; (line = in.readLine()) != null

builder.append(line).append("\n");
}
The getHttpClient function looks like:
public static final int HTTP_TIMEOUT = 30 * 1000;
private HttpClient mHttpClient;
private HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
If somebody could please help me out. It would be great.