4

I tried the code in android below:-

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://echo.jsontest.com/key/value/one/two");
        String text = null;


        try {
            HttpResponse response = httpClient.execute(httpGet, localContext);

            HttpEntity entity = response.getEntity();

            text = getASCIIContentFromEntity(entity);
            JSONArray jsonObj = new JSONArray(entity);
            System.out.print("message is"+jsonObj);
           }
        catch (Exception e) {
            return e.getLocalizedMessage();
        }
        return text;

        //Below i am displaying in my layout in my emulator

        protected void onPostExecute(String results) {
         if (results!=null) {
            TextView et = (TextView)findViewById(R.id.data);
            et.setText(results);
         }

         Button b = (Button)findViewById(R.id.getvehicles);
         b.setClickable(true);
       }

so when i just use getASCIIContentFromEntity(entity) ,then i am able to print full JSON data as it is.but instead i wanna print only the values inside this URL it is JSON data,please open to see it .

How can i fetch the values inside this json URL?

10
  • use some json parsing api like jackson, gson or simple-json.... : ) Commented Feb 27, 2015 at 9:45
  • First of all your json is not array its an object, so get it like this. JSONObject jsonObj = new JSONObject(entity.toString()); System.out.print("message is"+jsonObj.toString()); Commented Feb 27, 2015 at 9:49
  • @RemeesMSyde : should i import it from org.json.JSONOBJECT or org.json.simple.JSONOBJECT? Commented Feb 27, 2015 at 10:08
  • import org.json.JSONObject. Commented Feb 27, 2015 at 10:12
  • @RemeesMSyde : yes i tried the way you said brother, now i am getting a message on my emulator as Value org.apache.http.conn.BasicManagedEntity@350fe199 of type java.lang.String cannot be converted to JSONOBJECT. Commented Feb 27, 2015 at 10:32

1 Answer 1

2

Your response is in a JSONObject form so you have to get it like this.

HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
Object content = EntityUtils.toString(entity);
JSONObject jsonObj = new JSONObject(content.toString());

System.out.print("message is"+jsonObj.toString()); 

Then your JSONObject contains multiple values, you can get it by using the "key" of the pair.

String value = jsonObj.getString(key); //String value = jsonObj.getString("one");

In your case you want to print one and two, which means the kay and value, so you have to iterate the entire collection like this(If you know the keys used in the response no need of using this method).

Iterator<?> keys = jsonObj.keys();

     while( keys.hasNext() ){
          String key = (String)keys.next();
          System.out.print("key - value"+ key +   jsonObj.getString(key)); 
    }
Sign up to request clarification or add additional context in comments.

6 Comments

i understood your answer brother and i appreciate your help, but i have a small doubt if you could clear my concept.the line number 3 i think there you are converting the entity to string and storing in jsonObj and in line 4 you are again converting to string to print it !! do i again need to convert to string in line 4?? could you clear my this silly doubt brother.
Oopss, that is nothing bro. i just print the string after changed to a json object. You can print entity.toString() in the line before instead of this. jsonObj.toString() is json data and entity.toString() is the data coming from server as response.
And what to do if my json is an JSON array like this :- [{"vehicleId":1.0,"positionId":0.0,.................................................... How to extract???????
check this answer and read the comments, you can get the idea stackoverflow.com/questions/28758976/…
yes i saw it,but in here my json starts with square bracket itself how to get objects inside them
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.