2011-10-20 21 views
0

這個問題是一個跟進到我的前面一個問題可以在這裏找到:Android GSON: Parsing several different objects from the same response的Android GSON:對象未填充,但沒有出現錯誤

我跟着貼有答案,並創造4單獨的類爲我的JSON響應。 然而,當解析這個響應時,對象會發生(或者我認爲,因爲沒有解析錯誤發生),但是當試圖找出這些對象的數據時,會發生NullPointers。

我的JSON響應如下:

{ 
    "Home": { 
    "Icon": [ 
     { 
     "ScreenID": 533, 
     "ScreenIndex": 1, 
     "IconName": "mainIcon_news", 
     "Title": "News", 
     "FK_ModuleID": 6, 
     "FormID": 567, 
     "ModName": "News", 
     "MediaType": "", 
     "New_Icon": 0 
     }, 
     { 
     "ScreenID": 528, 
     "ScreenIndex": 2, 
     "IconName": "mainIcon_music", 
     "Title": "Music", 
     "FK_ModuleID": 3, 
     "FormID": 562, 
     "ModName": "Media", 
     "MediaType": "Music", 
     "New_Icon": 0 
     } 
    ], 
    "Header": [ 
     { 
     "ModHomeRotationID": 183, 
     "image_url": "*****/Media/68/1216_5.jpg", 
     "flg_RotationEnabled": false, 
     "flg_RotateOnlyOnReturn": true, 
     "flg_RotationRandomize": false, 
     "flg_RotationDelayMS": 5000, 
     "flg_RotationDelayFadeMS": 3000, 
     "HomeRotationIndex": null 
     } 
    ], 
    "Player": [ 
     { 
     "MediaID": 1219, 
     "Track_Name": "***", 
     "song_url": "*****/Media/68/1219.mp3", 
     "song_remote_url": null, 
     "FileSize": 4700502 
     }, 
     { 
     "MediaID": 1220, 
     "Track_Name": "**** ", 
     "song_url": "*****/Media/68/1220.mp3", 
     "song_remote_url": null, 
     "FileSize": 4350222 
     } 
    ] 
    } 
} 

我的代碼來解析這種反應是如下:

package com.mobowski.app.menusections; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import com.mobowski.app.json.WebService; 

import com.mobowski.app.listitems.Item; 
import com.google.gson.Gson; 

import android.app.Activity; 

import android.os.Bundle; 

//import android.widget.Toast; 

public class TestMain extends Activity { 

    ArrayList<Item> items = new ArrayList<Item>(); 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     // setContentView(R.layout.mainlinks); 
     retrieveLinks(); 

    } 

    public void retrieveLinks() { 
     // Instantiate the Web Service Class with he URL of the web service not 
     // that you must pass 

     WebService webService = new WebService(
       "http://editedduetosecurityreasons"); 

     // Pass the parameters 
     Map<String, String> params = new HashMap<String, String>(); 
     params.put("iAppID", "59"); 
     params.put("iUserID", "1"); 
     params.put("strCulName", ""); 
     // params.put("var", ""); 

     // Get JSON response from server the "" are where the method name would 
     // normally go if needed example 
     // webService.webGet("getMoreAllerts", params); 
     String response = webService.webGet("", params); 
     System.out.println("Returns: " + response); 

     try { 

      Home collection = new Gson().fromJson(response, Home.class); 

      for (Icon icon : collection.icons) { 
       System.out.println(icon.title); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public class Home { 

     public List<Icon> icons; 
     public Header header; 
     public Player player[]; 

     public List<Icon> getIcons() { 
      return icons; 
     } 

     public void setIcons(List<Icon> icons) { 
      this.icons = icons; 
     } 

     public Header getHeader() { 
      return header; 
     } 

     public void setHeader(Header header) { 
      this.header = header; 
     } 

     public Player[] getPlayer() { 
      return player; 
     } 

     public void setPlayer(Player[] player) { 
      this.player = player; 
     } 

     public Player getSinglePlayer(int i) { 
      return player[i]; 
     } 

     public Icon getSingleIcon(int i) { 
      return icons.get(i); 
     } 
    } 

    public class Icon { 

     public int ScreenID; 
     public int ScreenIndex; 
     public String IconName; 
     public String title; 
     public int FK_ModuleID; 
     public int FormID; 
     public String ModName; 
     public String MediaType; 
     public int New_Icon; 

     public int getScreenID() { 
      return ScreenID; 
     } 

     public void setScreenID(int screenID) { 
      ScreenID = screenID; 
     } 

     public int getScreenIndex() { 
      return ScreenIndex; 
     } 

     public void setScreenIndex(int screenIndex) { 
      ScreenIndex = screenIndex; 
     } 

     public String getIconName() { 
      return IconName; 
     } 

     public void setIconName(String iconName) { 
      IconName = iconName; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public void setTitle(String title) { 
      this.title = title; 
     } 

     public int getFK_ModuleID() { 
      return FK_ModuleID; 
     } 

     public void setFK_ModuleID(int fK_ModuleID) { 
      FK_ModuleID = fK_ModuleID; 
     } 

     public int getFormID() { 
      return FormID; 
     } 

     public void setFormID(int formID) { 
      FormID = formID; 
     } 

     public String getModName() { 
      return ModName; 
     } 

     public void setModName(String modName) { 
      ModName = modName; 
     } 

     public String getMediaType() { 
      return MediaType; 
     } 

     public void setMediaType(String mediaType) { 
      MediaType = mediaType; 
     } 

     public int getNew_Icon() { 
      return New_Icon; 
     } 

     public void setNew_Icon(int new_Icon) { 
      New_Icon = new_Icon; 
     } 

    } 

    public class Player { 

     public int MediaID; 
     public String Track_Name; 
     public String song_url; 
     public String song_remote_url; 
     public int FileSize; 

     public int getMediaID() { 
      return MediaID; 
     } 

     public void setMediaID(int mediaID) { 
      MediaID = mediaID; 
     } 

     public String getTrack_Name() { 
      return Track_Name; 
     } 

     public void setTrack_Name(String track_Name) { 
      Track_Name = track_Name; 
     } 

     public String getSong_url() { 
      return song_url; 
     } 

     public void setSong_url(String song_url) { 
      this.song_url = song_url; 
     } 

     public String getSong_remote_url() { 
      return song_remote_url; 
     } 

     public void setSong_remote_url(String song_remote_url) { 
      this.song_remote_url = song_remote_url; 
     } 

     public int getFileSize() { 
      return FileSize; 
     } 

     public void setFileSize(int fileSize) { 
      FileSize = fileSize; 
     } 

    } 

    public class Header { 

     public int ModHomeRotationID; 
     public String image_url; 
     public boolean flg_RotationEnabled; 
     public boolean flg_RotateOnlyOnReturn; 
     public boolean flg_RotationRandomize; 
     public int flg_RorationDelayMS; 
     public int flg_RotationDelayFadeMS; 
     public int HomeRotationIndex; 

     public int getModHomeRotationID() { 
      return ModHomeRotationID; 
     } 

     public void setModHomeRotationID(int modHomeRotationID) { 
      ModHomeRotationID = modHomeRotationID; 
     } 

     public String getImage_url() { 
      return image_url; 
     } 

     public void setImage_url(String image_url) { 
      this.image_url = image_url; 
     } 

     public boolean isFlg_RotationEnabled() { 
      return flg_RotationEnabled; 
     } 

     public void setFlg_RotationEnabled(boolean flg_RotationEnabled) { 
      this.flg_RotationEnabled = flg_RotationEnabled; 
     } 

     public boolean isFlg_RotateOnlyOnReturn() { 
      return flg_RotateOnlyOnReturn; 
     } 

     public void setFlg_RotateOnlyOnReturn(boolean flg_RotateOnlyOnReturn) { 
      this.flg_RotateOnlyOnReturn = flg_RotateOnlyOnReturn; 
     } 

     public boolean isFlg_RotationRandomize() { 
      return flg_RotationRandomize; 
     } 

     public void setFlg_RotationRandomize(boolean flg_RotationRandomize) { 
      this.flg_RotationRandomize = flg_RotationRandomize; 
     } 

     public int getFlg_RorationDelayMS() { 
      return flg_RorationDelayMS; 
     } 

     public void setFlg_RorationDelayMS(int flg_RorationDelayMS) { 
      this.flg_RorationDelayMS = flg_RorationDelayMS; 
     } 

     public int getFlg_RotationDelayFadeMS() { 
      return flg_RotationDelayFadeMS; 
     } 

     public void setFlg_RotationDelayFadeMS(int flg_RotationDelayFadeMS) { 
      this.flg_RotationDelayFadeMS = flg_RotationDelayFadeMS; 
     } 

     public int getHomeRotationIndex() { 
      return HomeRotationIndex; 
     } 

     public void setHomeRotationIndex(int homeRotationIndex) { 
      HomeRotationIndex = homeRotationIndex; 
     } 

    } 

} 

這段代碼似乎但是並沒有工作。調用for (Icon icon : collection.icons) {時發生空指針。在任何應該實際執行的項目上使用任何方法時,都會出現NullPointer,但Home對象除外。

我確定這意味着我做錯了什麼,但我不知道我做錯了什麼或在哪裏。有人能給我一些指點嗎?

在此先感謝的人誰願意給它一個嘗試


試圖Venky的建議之後,似乎現在的數據分析更好。不過,我仍然面臨着一個問題。雖然試圖解析頭對象,logcat的介紹我下面的錯誤:

com.google.gson.JsonParseException: Expecting object found: [{"ModHomeRotationID":162,"image_url":"***********/1020_5.jpg","flg_RotationEnabled":false,"flg_RotateOnlyOnReturn":true,"flg_RotationRandomize":false,"flg_RotationDelayMS":5000,"flg_RotationDelayFadeMS":3000,"HomeRotationIndex":null}] 

我知道這意味着它是期望接收的對象,卻得到了一個數組來代替。然而,我的代碼是這樣:

public class Header { 
     List<Header_info> header; 
    } 

    public class Header_info { 
     public int ModHomeRotationID; 
     public String image_url; 
     public boolean flg_RotationEnabled; 
     public boolean flg_RotateOnlyOnReturn; 
     public boolean flg_RotationRandomize; 
     public int flg_RorationDelayMS; 
     public int flg_RotationDelayFadeMS; 

    public int HomeRotationIndex; 
} 

我用同樣的方法爲圖標的對象,這些似乎解析罰款(沒有錯誤)。 任何建議爲什麼這不會爲Header對象工作?它似乎與圖標的格式相同。

+0

尚未對不起,我目前在後面在其他項目上工作的學校,當我回到家時,我會試着回答你的答案,這樣我就可以再次完成實習任務。 –

回答

1

檢查此GSON Response

驗證使用JSONLint

登記的logcat輸出上述響應:由於輸出用Log命令印刷:

輸出的同型被解析時,進行變更按您的需要。

只是檢查響應和團體反對

Java代碼:

Gson gson = new Gson(); 
RecordResponse cashResponse = null; 
try { 
cashResponse = gson.fromJson(response_data, RecordResponse.class); 
} 
catch (JsonSyntaxException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
catch (JsonIOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
List<Result> results = cashResponse.response.groups; 
for (Result cashResult : results){ 
for(Result_items rest : cashResult.items){ 
    if(rest!=null && !rest.equals("")){ 
     Log.v("IDSSSSS", rest.name); 
     Log.v("IDSSSSS", rest.id); 
     Log.v("IDSSSSS", rest.location.distance); 
     Log.v("IDSSSSS", rest.location.lat); 
     Log.v("IDSSSSS", rest.location.lng); 
     try{ 
      for(Result_category cat : rest.categories){ 
       Log.v("category_icon", cat.id); 
       Log.v("category_name", cat.icon); 
       Log.v("category_id", cat.name); 
      } 
     }catch(Exception e){} 

    } 
} 
} 

Correponding類文件獲取數據:

class RecordResponse{ 
    CashGamesContainer response; 
} 

    class CashGamesContainer{ 
    List<Result> groups; 
    } 

class Result{ 
List<Result_items> items; 
} 
class Result_items{ 
    String id; 
String name; 
String verified; 
Locations location; 
Stats stats; 
List<Result_category> categories; 
    } 

class Locations{ 
String address; 
String lat; 
String lng; 
String distance; 
    } 

    class Stats{ 
String checkinsCount; 
String usersCount; 
String tipCount; 
    } 

    class Result_category{ 
    String id; 
    String name; 
    String icon; 
    } 
+0

謝謝,我用你的例子,我認爲現在解析更好。不過,我仍然面臨一個問題。 我在第一篇文章中編輯了我的問題 –

+0

最終確定,由於響應也被大寫,所以主對象的字段名稱需要大寫。感謝您讓我朝着正確的方向! –