【发布时间】:2012-04-23 06:30:04
【问题描述】:
我正在尝试在 Android Java 环境中检索 POST 对 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())
然后我的应用程序崩溃了。
我做错了什么?
【问题讨论】:
-
请改用
System.out.println(e)。或e.printStackTrace()。或者至少System.out.println(e.getClass())。 -
@JonSkeet 你确定这行会在运行 Android 代码时显示吗?
-
可能在没有消息的情况下创建了异常。试试
e.printStackTrace(); -
Android 不显示标准控制台输出。使用
Log.e("TAG", e)(你需要导入android.util.Log)然后在LogCat中看到错误。 -
@BorisStrandjev:鉴于他当前的代码显然报告“死亡”,我假设 OP 已经涵盖了这一点。
标签: java android httprequest