Pages

Monday 22 September 2014

Easy way to get json response for both GET and POST method

For POST method:

   HttpResponse response;
JSONObject jsonObject = null;
// Simulates a background job.

DefaultHttpClient client = new DefaultHttpClient();
try {
HttpPost post = new HttpPost(www.google.com/api");

List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("sessionkey", "xxx"));

post.setEntity(new UrlEncodedFormEntity(params1));
response = client.execute(post);
jsonObject = new JSONObject(EntityUtils.toString(response
.getEntity()));

Log.d("result", jsonObject.toString() + "");

} catch (Exception e) {
e.printStackTrace();
}