DefaultHTTPClient抛出未知异常

时间:2012-04-23 06:30:05

标签: java android httprequest

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

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

android.util.Log类用于Logger ..

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