Reputation: 4792
I'm try to retrieve the response of a POST to a URL in an Android Java Environment:
Here's my code:
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");
}
Sure enough, it returns "DIED".
If I change System.out.println("DIED");
to: System.out.println(e.getMessage())
then my application crashes.
What am I doing wrong?
Upvotes: 0
Views: 497
Reputation: 109237
Use android.util.Log
class for Logger..
catch(Exception e)
{
Log.e("Exception:",e.toString());
// or
e.printStackTrace();
}
Upvotes: 1