php会话变量在android应用程序中重置

时间:2016-06-27 17:54:04

标签: php android session android-studio get

我在php脚本中使用会话变量,我在Android应用程序中使用它来存储有关用户的一些信息。当我在终端中运行php脚本时,此实现有效。但是,当我尝试在android studio中运行我的应用程序时,php会话变量会在调用之间重置。我如何确保在Android应用程序中的调用之间维护我的php会话?

我做了一些研究,我相信问题出现在下面的代码中(这是我创建get请求的地方):

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;
}

我已经尝试将httpClient,cookieStore和localContext变量设为静态,但php Session变量仍然被重置。

我正在实例化我的GetRequest对象:

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;
}

请帮助我!!

1 个答案:

答案 0 :(得分:0)

好的,问题结果比预期的要简单。我所要做的就是创建一个静态的HttpClient对象,我每次都会重用它。

无需使用CookieStores或HttpContext。

此代码有效:

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;
}