默认 HTTP 引发未知异常



>我正在尝试检索 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");
            }

果然,它返回"死亡"。如果我将System.out.println("DIED");更改为:System.out.println(e.getMessage())然后我的应用程序崩溃了。

我做错了什么?

android.util.Log类用于记录器。

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

最新更新