Skip to content

Instantly share code, notes, and snippets.

@adilmughal
Created August 29, 2012 06:41
Show Gist options
  • Save adilmughal/3507547 to your computer and use it in GitHub Desktop.
Save adilmughal/3507547 to your computer and use it in GitHub Desktop.
Android Http Client Usage To Access JSON Service
DefaultHttpClient client = new DefaultHttpClient();
// http get request
HttpGet request = new HttpGet(EMPLOYEE_SERVICE_URI + evEmployeeId.getText());
// set the hedear to get the data in JSON formate
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// get the response
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// if entity contect lenght 0, means no employee exist in the system with these code
if (entity.getContentLength() != 0) {
// stream reader object
Reader employeeReader = new InputStreamReader(response.getEntity()
.getContent());
// create a buffer to fill if from reader
char[] buffer = new char[(int) response.getEntity()
.getContentLength()];
// fill the buffer by the help of reader
employeeReader.read(buffer);
// close the reader streams
employeeReader.close();
// for the employee json object
JSONObject employee = new JSONObject(new String(buffer));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment