1

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!!

0

1 Answer 1

1

Ok so the problem turned out to be simpler than expected. All I had to do was make a static HttpClient object that I would reuse each time.

No need to fool around with CookieStores or HttpContext.

This code works:

public class GetRequest extends AsyncTask<String, String, String> {



//initialize the httpClient to null so that we can check if it has been updated
//having one global httpClient allows us to use PHP session variables.
public static HttpClient httpClient = null;

protected String doInBackground(String... params) {

    String response = null;

    String pUrl = params[0];

    try {

        URL url = new URL(pUrl);

        Log.v("URL", "URL " + url.toString());

        if(httpClient == null) {//if the httpClient has not yet been set....
            HttpClient httpClient = new HttpClient();//Create the HttpClient that will be reused each time

        }

        HttpGet httpGet = new HttpGet(url.toURI());

        httpGet.setHeader("Accept", "application/json");

        HttpResponse httpResponse = httpClient.execute(httpGet);
        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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.