hit
hit

Reputation: 25

working with JSON data. not able to parse the with the following code in android

By the following code i am not able to parse the data from the URL

try{

  HttpClient client= new DefaultHttpClient();

            HttpGet http= new HttpGet("http://api.androidhive.info/contacts/");

            HttpResponse response = client.execute(http);

            HttpEntity httpentity = response.getEntity();

            is= httpentity.getContent();

        }catch (ClientProtocolException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 0

Views: 81

Answers (3)

Ajeesh
Ajeesh

Reputation: 1646

If you are trying to retrieve the data then you can use the below code which converts the response into a string value,

HttpClient client= new DefaultHttpClient();
HttpPost http=new HttpPost("http://api.androidhive.info/contacts/");
HttpResponse response = client.execute(http);
HttpEntity httpentity = response.getEntity();
String result=EntityUtils.toString(httpentity);

Use this result value for parsing JSON. You could use org.json jar file which contains a constructor for JSONArray with String as an argument.

Eg. JSON Parsing

JSONArray jarray=new JSONArray(result);
for(int i=0;i<jarray.length();i++)
{
   JSONObject jobject=jarray.getJSONObject(i);
   System.out.println(jobject.getString("test"));
}

In your case you need to parse first JSONObject instead of JSONArray.

Upvotes: 1

Brijesh Patel
Brijesh Patel

Reputation: 676

Do this way.. This is my working code for getiing json response from url using in many app.

 /**
 * This method used to get json from url.
 * 
 * @param url
 *            represented url
 * @return represented {@link JSONObject}
 */
public JSONObject getJSONFromUrl(String url) {

    InputStream is = null;
    JSONObject jObj = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        jObj = new JSONObject(getResponseBody(is));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return jObj;
}

/**
 * This method used to get response body.
 * 
 * @param instream
 *            represented input stream
 * @return represented {@link String}
 * @throws IOException
 *             represented {@link IOException}
 * @throws ParseException
 *             represented {@link ParseException}
 */
public String getResponseBody(final InputStream instream) throws IOException, ParseException {

    if (instream == null) {
        return "";
    }

    StringBuilder buffer = new StringBuilder();

    BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "utf-8"));

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

    } finally {
        instream.close();
        reader.close();
    }
    System.out.println(buffer.toString());
    return buffer.toString();

}

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93862

As you didn't stated what part of the parsing you don't understand, you can read this tutorial which show how to parse exactly the same JSON data as you :

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Upvotes: 1

Related Questions