I am using a session variable in a php script that I am using in an Android application to store some information about a user. This implementation works when I run the php script in my terminal. However, when I try to run my application in Android Studio, the php session variable gets reset in between calls. How do I ensure that my php session is maintained in between calls in my android application?
I have done some research and I believe the problem lies somewhere in the code below (This is where I am creating my get request):
public class GetRequest extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
String response = null;
String pUrl = params[0];
try {
URL url = new URL(pUrl);
httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpGet = new HttpGet(url.toURI());
httpGet.setHeader("Accept", "application/json");
HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
if (response != null) {
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return response;
}
I have tried making the httpClient, cookieStore, and localContext variables static but the php Session variable was still reset.
I am instantiating my GetRequest objects like this:
public static String serverAsyncRequestGet (String params, String api) {
String output = null;
GetRequest gr = new GetRequest();
try {
output = gr.execute(api + params).get();
}
catch (Exception e) {}
return output;
}
Please help me!!