我遇到Android HttpPost请求问题。这样的代码:关于Android HttpPost请求
@Override
public void onClick(View v) {
NameValuePair nameValuePair1 = new BasicNameValuePair("name", "zhangsan");
NameValuePair nameValuePair2 = new BasicNameValuePair("age", "18");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try {
HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
HttpPost httpPost = new HttpPost("http://www.*.cn/test.aspx");
httpPost.setEntity(requestHttpEntity);
HttpClient httpClient = new DefaultHttpClient();
//Using the Http client sends request object
try {
System.out.println("1111111111111");
httpResponse = httpClient.execute(httpPost);
System.out.println("2222222222222");
httpEntity = httpResponse.getEntity();
System.out.println("33333333333333");
System.out.println(EntityUtils.toString(httpResponse.getEntity()));
System.out.println("44444444444444");
} catch (Exception e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
只要通过考试,该程序可以运行在了Android2.3和模拟器无problemly,最终得到正确的输出。
它运行上的Android2.1和模拟器或Android2.2的将在以下位置stucked。
System.out.println (EntityUtils.toString (httpResponse.getEntity()));
等待很长一段时间,它occasionaliy弹出的应用程序的提示是无响应,偶尔出现的"44444444444444"
输出。但是没有在正常情况下得到的功能"System.out.println (EntityUtils.toString (httpResponse.getEntity()));"
的输出。
所以,我没有分别Android2.2的手机上的几个测试与仿真器和Android2.1的手机模拟器。
测试一:
我改变http://www.*.cn/test.aspx
为http://www.google.com
,并正确运行。
测试二:
我注释掉的"httpPost.setEntity (requestHttpEntity); "
行,然后请求的URL没有TNE代码的请求参数。它运行正常。
请给我一个建议,谢谢!