Convert an unnamed JSONArray to a list using retrofit
I am trying to populate a List object using a modification. I am currently getting a null pointer exception whenever I call the List object. How can I make the revision correct?
My call for retrofitting:
@Override
public void success(List<Game> gameList, Response response) {
mGameSeason = gameList;
}
My implementation:
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);
}
}
My POJO game:
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 response:
[
{
"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"
}
]
+3
source to share
2 answers
Are you declaring your mGameSeason list correctly before? You can try doing something like this
public void success(List<Game> gameList, Response response) {
mGameSeason.clear();
mGameSeason.addAll(gameList);
gameAdapter.notifyDataSetChanged();
}
You will also need to define an adapter for your list if you haven't already.
Let me know if this helps
Here you can do for adapter
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);
}
}
0
source to share
RestAdapter and your API should be used as SINGLETON, it looks like you are not checking the residual adapter as single, try something like this in your fragment or activity:
// 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;
}
0
source to share