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

4
  • different json maybe? Commented May 21, 2013 at 9:19
  • what is the problem exactly ? Does it crash? Fields are empty ? Commented May 21, 2013 at 9:19
  • yes, fields are empty! the json is different but i modified it.. Commented May 21, 2013 at 9:20
  • Writing it "won't work" doesn't help at all in understanding the problem. Post your error log. Also, we need more code to understand what you are trying to do. Commented May 21, 2013 at 9:21

2 Answers 2

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":"[email protected]",
         "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"
      }
   ]
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.