2011-10-20 4 views
0

を発生しません:Android GSON: Parsing several different objects from the same responseアンドロイドGSON:満たされていないオブジェクトが、エラーがこの質問をここで見つけることができます私の以前の質問にフォローアップされ

私はそこに掲載の答えに続いて、4つの別々のクラスを作成しました私のJSONレスポンスのために。 しかし、この応答を解析すると、オブジェクトが生成されます(または解析エラーが発生しないためだと思います)。しかし、これらのオブジェクトのデータを検索しようとすると、NullPointerが発生します。

次のように私の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; 
     } 

    } 

} 

しかし、このコードが動作するようには思えません。 nullポインタは、for (Icon icon : collection.icons) {が呼び出されたときに発生します。 NullPointerは、Homeオブジェクトを除いて、実際に作成されていなければならない項目のいずれかでメソッドが使用されたときに発生します。

私はこれが何か間違ったことを意味すると確信していますが、私は何が間違っていたのか、どこではなかったのか分かりません。誰かが私にいくつかの指針を与えることができますか?それをVenkyの提案を試みた後


を試しを与えるために喜んでいる人のため事前に

おかげで、データが良くなりまし解析しているようです。しかし、私はまだ問題に直面しています。

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

私はこれは、オブジェクトを受け取るために期待していたことを知っているが、その代わりに、配列を得た:Headerオブジェクトを解析しようとして、LogCatは私に次のエラーを表示します。

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の出力をチェックして下さい:出力ログのコマンドを使用して印刷するので:出力の

同じ型が解析され、あなたのあたりのような変更を行いますニーズ。

だけ応答やグループをチェックオブジェクト

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

最終的には、主要なオブジェクトのフィールド名は大文字でもあるので、大文字にする必要がありました。しかし、正しい方向に私を得るためにありがとう! –

関連する問題