Venky
Venky

Reputation: 2047

Need to get JSONObjects from this

This is my URL "http://182.18.161.240:7070/sfaweb-1.1.015/get/retailers/dl-0004/2"

I was able to download the data through volley, but while parsing I'm not able to retrieve anything from this. Help would be deeply appreciated.

Response is nothing but the downloaded data.

JSONObject jsonObject=new JSONObject(response);

for(int i=0;i<jsonObject.length();i++) {
    //JSONObject jsonObject1=jsonArray.getJSONObject(i);
    String rtrname=jsonObject.getString("rtrname").toString();
    String ctgname=jsonObject.getString("ctgname").toString();
    String rtrphoneno=jsonObject.getString("rtrphoneno").toString();

    str+= "\n rtrname:"+rtrname+"\n ctgname:"+ctgname+"\n rtrphoneno:" +rtrphoneno+"\n";
    boolean isInserted = database.insertData(rtrname,ctgname,rtrphoneno);
}

What am I missing in this? I have been stuck on this for quite some time, so please help me. I'm kinda new to this.

Upvotes: 3

Views: 51

Answers (2)

Vickyexpert
Vickyexpert

Reputation: 3167

Try Below Code,

   JSONArray jsonArray = new JSONArray(response);       

  for(int i=0;i < jsonArray.length(); i++) 
  {
    JSONObject jsonObject= jsonArray.getJSONObject(i);

     //JSONObject jsonObject1=jsonArray.getJSONObject(i);
     String rtrname=jsonObject.getString("rtrname").toString();
     String ctgname=jsonObject.getString("ctgname").toString();
     String rtrphoneno=jsonObject.getString("rtrphoneno").toString();

     str+= "\n rtrname:"+rtrname+"\n ctgname:"+ctgname+"\n rtrphoneno:" +rtrphoneno+"\n";
     boolean isInserted = database.insertData(rtrname,ctgname,rtrphoneno);
  }

Upvotes: 0

Dennis van Opstal
Dennis van Opstal

Reputation: 1338

The response you are getting is an array so you should put it in an array:

JSONArray jsonArray = new JSONArray(response);

Upvotes: 2

Related Questions