2013-05-21 67 views
0

爲了我一直在使用下面的代碼來創建一個列表視圖:問題的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"; 

這一個以上的作品,但如果我把它變成:

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"; 

它不會工作now.I不理解可能是什麼問題,它應該工作,相同的結構相同的東西... 謝謝

+0

不同的JSON? – Blackbelt

+0

究竟是什麼問題?它會崩潰嗎?字段是空的? – Gomoku7

+0

是的,領域是空的! JSON是不同的,但我修改了它.. – alandr

回答

1

JSON結構是不同的。

這是第一個結構的樣子(我只保留了一個記錄簡潔):

{ 
    "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" 
     } 
     } 
    ] 
} 

這是第二個結構是怎樣的樣子:

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

的結構在兩種情況下JSON是不同的。 在working case你有

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

雖然在second case(你不工作的代碼)不具有等效的子標籤。因此,當您使用第一個代碼解析第二個JSON時,您的代碼應該會失敗並出現解析錯誤。

有關如何在Android的JSON解析正確的認識,有一個看看JSONReaderHow to parse JSON in Android

也許