我正在尝试使用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();
}
}
你能帮我解决这个问题吗?