Thursday, 7 August 2014

get the responce from server in json post android ?

public static <JSONObject> String postRequest(String url,
List<NameValuePair> postData, Context context) {

String response = null;

try {
HttpParams httpParameters = new BasicHttpParams();

int timeoutConnection = 20000;

HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);

int timeoutSocket = 20000;

HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

HttpClient httpclient = new DefaultHttpClient(httpParameters);

HttpPost httppost = new HttpPost(url);

httppost.setEntity(new UrlEncodedFormEntity(postData));

HttpResponse httpResponse = null;

try {
httpResponse = httpclient.execute(httppost);

response = EntityUtils.toString(httpResponse.getEntity());

System.out.println("ResponceData" + response);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ConnectTimeoutException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {

Toast.makeText(context, "Poor network connection", 2000).show();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}

return response;
}

No comments:

Post a Comment