Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a JSON object from a HTTP response

Tags:

java

json

android

I want to get a JSON object from a Http get response:

Here is my current code for the Http get:

protected String doInBackground(String... params) {      HttpClient client = new DefaultHttpClient();     HttpGet request = new HttpGet(params[0]);     HttpResponse response;     String result = null;     try {         response = client.execute(request);                  HttpEntity entity = response.getEntity();          if (entity != null) {              // A Simple JSON Response Read             InputStream instream = entity.getContent();             result = convertStreamToString(instream);             // now you have the string representation of the HTML request             System.out.println("RESPONSE: " + result);             instream.close();             if (response.getStatusLine().getStatusCode() == 200) {                 netState.setLogginDone(true);             }          }         // Headers         org.apache.http.Header[] headers = response.getAllHeaders();         for (int i = 0; i < headers.length; i++) {             System.out.println(headers[i]);         }     } catch (ClientProtocolException e1) {         // TODO Auto-generated catch block         e1.printStackTrace();     } catch (IOException e1) {         // TODO Auto-generated catch block         e1.printStackTrace();     }     return result; } 

Here is the convertSteamToString function:

private static String convertStreamToString(InputStream is) {      BufferedReader reader = new BufferedReader(new InputStreamReader(is));     StringBuilder sb = new StringBuilder();      String line = null;     try {         while ((line = reader.readLine()) != null) {             sb.append(line + "\n");         }     } catch (IOException e) {         e.printStackTrace();     } finally {         try {             is.close();         } catch (IOException e) {             e.printStackTrace();         }     }     return sb.toString(); } 

Right now I am just getting a string object. How can I get a JSON object back.

like image 535
Zapnologica Avatar asked Aug 06 '13 07:08

Zapnologica


People also ask

How do you get data from a response object?

Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

How do you get a JSON object from a RESTful web service?

First, a String URL to call the RESTful Web Service, and second, the name of the class should return with the response. So, in just one line of code, it calls the RESTful web service, parses the JSON response, and returns the Java object to you.


1 Answers

The string that you get is just the JSON Object.toString(). It means that you get the JSON object, but in a String format.

If you are supposed to get a JSON Object you can just put:

JSONObject myObject = new JSONObject(result); 
like image 81
Renan Bandeira Avatar answered Sep 24 '22 00:09

Renan Bandeira