java如何在Android 4.0中从HttpClient获取响应
当我在安卓4.0中使用这段代码时,我遇到了一些麻烦,我没有得到响应,但在2.2,2.3,2.3.3版本中得到了响应
List<NameValuePair> pwadd = new ArrayList<NameValuePair>();
pwadd.add(new BasicNameValuePair("UName", UName));
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(
"http://10.0.2.2/new/webser/Load.php");
httppost.setEntity(new UrlEncodedFormEntity(pwadd));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String response = sb.toString();
要在安卓 4.0版本中运行,我应该在这段代码中做些什么
# 1 楼答案
在异步任务中尝试这些。无论如何,你需要在一个单独的线程中创建网络内容
希望对你有帮助