First, in Android cant do network tasks in the main thread, you must do it async
Create this Class
private class SomeServiceRequest extends AsyncTask<JSONObject, Long, JSONObject> {
@Override
protected JSONObject doInBackground(JSONObject... params) {
HttpPost request = new HttpPost(SERVICE_URI);
request.setHeader("Accept", "application/json; charset=utf-8");
request.setHeader("Content-type", "application/json; charset=utf-8");
JSONObject result = null;
// Build JSON string
try {
StringEntity entity = new StringEntity(params[0].toString(),
"UTF-8");
request.setEntity(entity);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
StatusLine sl = response.getStatusLine();
if (sl.getStatusCode() == 200) {
result = new JSONObject(EntityUtils.toString(response
.getEntity()));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(JSONObject result) {
processSvcResult(result);
super.onPostExecute(result);
}
}
This code for process the response
private void processSvcResult(JSONObject result) {
if (result != null) {
try {
Boolean success = result.getString("Status").equals("OK");
storeRegistrationSuccess(context, success);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
For use it sample
private void CallSomeService(String messageId) {
final SharedPreferences prefs = getSharedPreferences(
this.getClass().getSimpleName(), Context.MODE_PRIVATE);
// PROPERTY_IDDEVICE
String iddevice = prefs.getString(PROPERTY_IDDEVICE, "");
JSONObject p = new JSONObject();
try {
p.put("IDDevice", iddevice.toLowerCase(Locale.ROOT));
p.put("MessageID", messageId);
new SomeServiceRequest().execute(p);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}