0

I have been using the following code in order to create a listview:

public class AndroidJSONParsingActivity extends ListActivity {


// url to make request
private static String url = "http://api.androidhive.info/contacts/";

// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";

This one above works, but if I change it into:

private static String url = "http://kondicioner.al/app/json.php";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "emer";
    private static final String TAG_EMAIL = "mbiemer";
    private static final String TAG_ADDRESS = "telefon";
    private static final String TAG_GENDER = "adresa";
    private static final String TAG_PHONE = "ora";
    private static final String TAG_PHONE_MOBILE = "per";
    private static final String TAG_PHONE_HOME = "dyqan";
    private static final String TAG_PHONE_OFFICE = "statusi";

It won't work now.I don't understand what may be the problem, it's supposed to work, same structure same thing... Thanks

krammer
  • 2,598
  • 2
  • 25
  • 46
alandr
  • 43
  • 5

2 Answers2

1

The JSON structures are different.

This is how the first structure looks like (I have retained only one record for brevity):

{
   "contacts":[
      {
         "id":"c200",
         "name":"Ravi Tamada",
         "email":"ravi@gmail.com",
         "address":"xx-xx-xxxx,x - street, x - country",
         "gender":"male",
         "phone":{
            "mobile":"+91 0000000000",
            "home":"00 000000",
            "office":"00 000000"
         }
      }
   ]
}

And this is how the second structure looks like:

{
   "contacts":[
      {
         "id":"12",
         "emer":"Albana",
         "mbiemer":"",
         "telefon":"",
         "adresa":"",
         "ora":"10:13:44",
         "per":"",
         "dyqan":"",
         "statusi":"",
         "orari_transportit":"",
         "data":"15\/4\/2013"
      }
   ]
}
Rajesh
  • 15,724
  • 7
  • 46
  • 95
0

The structure of the JSON in the two cases is different. In the working case you have

"phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }

While in the second case (your non working code) doesn't have equivalent subtags. So when you would parse the second JSON using the code for the first one, your code is supposed to fail with parsing errors.

For a proper understanding of how to parse JSON in Android, have a look at the JSONReader or How to parse JSON in Android

Community
  • 1
  • 1
krammer
  • 2,598
  • 2
  • 25
  • 46