gpt4 book ai didi

java - DefaultHTTPClient 抛出未知异常

转载 作者:行者123 更新时间:2023-12-01 15:25:05 25 4
gpt4 key购买 nike

我尝试在 Android Java 环境中检索对 URL 的 POST 响应:

这是我的代码:

          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())然后我的应用程序崩溃了。

我做错了什么?

最佳答案

使用android.util.Log类作为记录器..

catch(Exception e)
{
Log.e("Exception:",e.toString());
// or
e.printStackTrace();
}

关于java - DefaultHTTPClient 抛出未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10275814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com