Skip to content

Instantly share code, notes, and snippets.

@eppsteve
Created September 23, 2015 18:27
Show Gist options
  • Select an option

  • Save eppsteve/3d307ee776f86d4ee7da to your computer and use it in GitHub Desktop.

Select an option

Save eppsteve/3d307ee776f86d4ee7da to your computer and use it in GitHub Desktop.
Get JSON from rest web service in android
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{
if(inputStream != null)
inputStream.close();
}catch(Exception squish){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment