I have following JSON returned from server
{
"matches": [
{
"unique_id": 1122278,
"date": "2018-01-24T00:00:00.000Z",
"team-2": "India",
"team-1": "South Africa",
"type": "Test",
"dateTimeGMT": "2018-01-24T08:00:00.000Z",
"squad": true,
"toss_winner_team": "India",
"matchStarted": true
},
{
"unique_id": 1116929,
"team-2": "India Under-19s",
"team-1": "Bangladesh Under-19s",
"type": "YouthODI",
"date": "2018-01-25T00:00:00.000Z",
"dateTimeGMT": "2018-01-25T21:30:00.000Z",
"squad": true,
"toss_winner_team": "India Under-19s",
"winner_team": "India Under-19s",
"matchStarted": true
},
{
"unique_id": 1115781,
"team-2": "England",
"team-1": "New Zealand",
"type": "Test",
"date": "2018-03-29T00:00:00.000Z",
"dateTimeGMT": "2018-03-29T22:00:00.000Z",
"squad": false,
"matchStarted": false
}
],
"v": "1",
"ttl": 43,
"provider": {
"source": "Various",
"url": "https://cricapi.com/",
"pubDate": "2018-01-27T13:18:55.717Z"
},
"creditsLeft": 250
}
Using Volley library and gson.
I am trying to push JSON array match to Match Model(Pojo). I think I can insert it using a loop, but there any other way to add all JSON array to model?
public class Main2Activity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Match> matches;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recylcer_1);
recyclerView=(RecyclerView) findViewById(R.id.recyler);
myAdapter=new MyAdapter(matches);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAnimation(n);
recyclerView.setAdapter(myAdapter);
callCricket();
}
public void callCricket() {
CallApi callApi = new CallApi();
callApi.setVolleyInterface(new VolleyInterface() {
@Override
public void onSucess(String string) {
Log.d("ApiCall_success", string);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Match>>(){}.getType();
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(string);
JSONArray jsonArray = jsonObject.getJSONArray("matches");
matches.addAll(gson.fromJson(jsonArray,type));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(String string) {
}
});
}
}
I have tried using gson but I got an error.