我正在尝试检索 POST 对 Android Java 环境中 URL 的响应:
这是我的代码:
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://myurl.com/post.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
JSONObject jsonObject = new JSONObject();
jsonObject.put("data1", "OK");
jsonObject.put("data2", "OK2");
nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString()));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String response = httpClient.execute(postMethod,resonseHandler);
System.out.println(response);
}
catch(Exception e)
{
System.out.println("DIED");
}
果然,它返回“DIED”。如果我更改System.out.println("DIED");
为:System.out.println(e.getMessage())
然后我的应用程序崩溃。
我究竟做错了什么?