在Android中维护会话无法正常工作

时间:2016-02-11 16:21:24

标签: java php android session httprequest

我正在尝试使用android中的会话维护登录信息。 我确信我服务器上的所有代码都可以,因为我已经使用普通的Web浏览器和android webview测试了它们。

我在stackoverflow上检查了很多类似的问题,但到目前为止还没有一个对我有效。

这就是我所做的......

@Override
protected String doInBackground(String... urls) {

try {
    //DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpClient httpClient = new DefaultHttpClient();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    HttpPost httpPost = new HttpPost(urls[0]);
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    //HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpResponse httpResponse = httpClient.execute(httpPost, httpContext);
    HttpEntity httpEntity = httpResponse.getEntity();
    InputStream is = httpEntity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    StringBuilder sb = new StringBuilder();
    int ch;
    while ((ch = reader.read()) != -1)
        sb.append((char) ch);
    Content = sb.toString();

} catch (IOException e) {
    Log.e("JsonError", e.getMessage());
}

    return Content;
}

@Override
protected void onPostExecute(String page_output) {
Dialog.dismiss();
try {
    Log.i("data", page_output);

    TextView tv = (TextView) context.findViewById(R.id.textView);
    tv.setText(Content);
} catch (Exception e) {
    e.printStackTrace();
}
}
你能帮我解决这个问题吗?

0 个答案:

没有答案