使用Retrofit将无名JSONArray转换为List

时间:2015-05-06 08:50:09

标签: android retrofit

我正在尝试使用改造来填充List对象。目前,每当我调用List对象时,我都会收到空指针异常。如何让改装工作正常?

我对Retrofit的呼吁:

@Override
public void success(List<Game> gameList, Response response) {
    mGameSeason = gameList;
}

My Retrofit实施:

public class ApiClient {
    private static ApiInterface sApiService;

    public static ApiInterface getApiClient() {
        if (sApiService == null) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://www.someapi.com")
                .build();

            sApiService = restAdapter.create(ApiInterface.class);
        }
        return sApiService;
    }

    public interface ApiInterface {
        @GET("path")
        void getGames(Callback<List<Game>> callback);
    }
}

我的游戏POJO:

public class Game {

    @Expose
    private String gameID;
    @Expose
    private String date;
    @Expose
    private String awayTeam;
    @Expose
    private String homeTeam;
    @Expose
    private String gameType;

    /**
     *
     * @return
     * The gameID
     */
    public String getGameID() {
        return gameID;
    }

    /**
     *
     * @param gameID
     * The gameID
     */
    public void setGameID(String gameID) {
        this.gameID = gameID;
    }

    /**
     *
     * @return
     * The date
     */
    public String getDate() {
        return date;
    }

    /**
     *
     * @param date
     * The date
     */
    public void setDate(String date) {
        this.date = date;
    }

    /**
     *
     * @return
     * The awayTeam
     */
    public String getAwayTeam() {
        return awayTeam;
    }

    /**
     *
     * @param awayTeam
     * The awayTeam
     */
    public void setAwayTeam(String awayTeam) {
        this.awayTeam = awayTeam;
    }

    /**
     *
     * @return
     * The homeTeam
     */
    public String getHomeTeam() {
        return homeTeam;
    }

    /**
     *
     * @param homeTeam
     * The homeTeam
     */
    public void setHomeTeam(String homeTeam) {
        this.homeTeam = homeTeam;
    }

    /**
     *
     * @return
     * The gameType
     */
    public String getGameType() {
        return gameType;
    }

    /**
     *
     * @param gameType
     * The gameType
     */
    public void setGameType(String gameType) {
        this.gameType = gameType;
    }
}

JSON响应:

[  
   {  
      "gameID":"2011030416",
      "date":"Mon Jun 11, 2012",
      "awayTeam":"New Jersey Devils",
      "homeTeam":"Los Angeles Kings",
      "gameType":"Playoffs"
   },
   {  
      "gameID":"2011030415",
      "date":"Sat Jun 09, 2012",
      "awayTeam":"Los Angeles Kings",
      "homeTeam":"New Jersey Devils",
      "gameType":"Playoffs"
   },
   {  
      "gameID":"2011030414",
      "date":"Wed Jun 06, 2012",
      "awayTeam":"New Jersey Devils",
      "homeTeam":"Los Angeles Kings",
      "gameType":"Playoffs"
   },
   {  
      "gameID":"2011030413",
      "date":"Mon Jun 04, 2012",
      "awayTeam":"New Jersey Devils",
      "homeTeam":"Los Angeles Kings",
      "gameType":"Playoffs"
   },
   {  
      "gameID":"2011030314",
      "date":"Mon May 21, 2012",
      "awayTeam":"New York Rangers",
      "homeTeam":"New Jersey Devils",
      "gameType":"Playoffs"
   },
   {  
      "gameID":"2011030313",
      "date":"Sat May 19, 2012",
      "awayTeam":"New York Rangers",
      "homeTeam":"New Jersey Devils",
      "gameType":"Playoffs"
   }
]

2 个答案:

答案 0 :(得分:0)

你之前是否正确宣布了你的名单mGameSeason? 你可以尝试做这样的事情

&#13;
&#13;
public void success(List<Game> gameList, Response response) {
            mGameSeason.clear();
            mGameSeason.addAll(gameList);
            gameAdapter.notifyDataSetChanged();
}
&#13;
&#13;
&#13;

如果您还没有为您的列表定义适配器,还需要定义适配器。

如果有帮助,请告诉我

您可以为适配器做些什么

&#13;
&#13;
protected List<Game> mGameList;
protected LayoutInflater inflater;
protected Context mContext;

  public GameAdapter(List<Game> games, Context context) {
    this.mGameList = games;
    this.mContext = context;
    this.inflater = LayoutInflater.from(this.mContext);
  }

@Override
    public int getCount() {
        return mGameList.size();
    }

    @Override
    public Game getItem(int position) {
        return mGameList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderItem viewHolderItem;

        if(convertView==null){
            // inflate the layout
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.list_item_game, parent, false);

            // well set up the ViewHolder
            viewHolderItem = new ViewHolderItem(convertView);

            // store the holder with the view.
            convertView.setTag(viewHolderItem);

        }else{
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolderItem = (ViewHolderItem) convertView.getTag();
        }

        Game game = getItem(position);

        // assign values if the object is not null
        if(game != null) {
          // set your layout here  
          viewHolderItem.nameGame.setText(game.getName());
        }
        return convertView;
    }

  static class ViewHolderItem {
    @InjectView(R.id.name_radio)
    TextView nameRadio;

    public ViewHolderItem(View v) {
      ButterKnife.inject(this, v);
    }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

RestAdapter和您的API接口应该用作SINGLETON,看起来您没有将其余的适配器检查为单例,在您的片段或活动中尝试这样的事情:

    // Activity or fragment use
MyAwesomeApiInterface api;

MyAwesomeApiInterface getApi() {
    if (api == null) {
        api = getRestAdapter().create(MyAwesomeApiInterface.class);
    }
    return api;
}

Retrofit retrofit;

Retrofit getRestAdapter() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}