I want to filter the specific json data in android studio,instead of getting all the data.For example filter the data based on 16-3-2019 date.Below is the json data
JSON
matches
0
unique_id : 1168120
date : "2019-03-15T00:00:00.000Z"
dateTimeGMT : "2019-03-15T04:30:00.000Z"
team-1 : "Afghanistan"
team-2 : "Ireland"
type : "Test"
squad : true
toss_winner_team : "Ireland"
winner_team : "Afghanistan"
matchStarted : true
1
unique_id : 1160301
date : "2019-03-16T00:00:00.000Z"
dateTimeGMT : "2019-03-16T21:30:00.000Z"
team-1 : "Northern Districts"
team-2 : "Central Districts"
toss_winner_team : "Northern Districts"
type : "First-class"
squad : false
matchStarted : true
What I have tried:
Below is the java code
<pre>public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<Model> modelList;
private String url = "http://cricapi.com/api/matches?apikey=wnUZ9BJ9keX6It8FQQryK6mbCZo1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
modelList = new ArrayList<>();
loadUrlData();
}
private void loadUrlData() {
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Loading ...");
pd.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pd.dismiss();
try {
JSONArray jsonArray=new JSONObject(response).getJSONArray("matches");
for (int i=0; i<jsonArray.length(); i++){
try {
String uniqueid=jsonArray.getJSONObject(i).getString("unique_id");
String team1=jsonArray.getJSONObject(i).getString("team-1");
String team2=jsonArray.getJSONObject(i).getString("team-2");
String matchType=jsonArray.getJSONObject(i).getString("type");
String matchStatus=jsonArray.getJSONObject(i).getString("matchStarted");
if (matchStatus.equals("true")){
matchStatus="Match Started";
}else {
matchStatus="Match not started";
}
String dateTimeGMT=jsonArray.getJSONObject(i).getString("dateTimeGMT");
SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format1.setTimeZone(TimeZone.getTimeZone(dateTimeGMT));
Date date=format1.parse(dateTimeGMT);
SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd HH:mm");
format2.setTimeZone(TimeZone.getTimeZone("GMT"));
String dateTime=format2.format(date);
Model model=new Model(uniqueid,team1,team2,matchType,matchStatus,dateTime);
modelList.add(model);
}catch (Exception e){
Toast.makeText(MainActivity.this,"" +e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
adapter=new MyAdapter(modelList,getApplicationContext());
recyclerView.setAdapter(adapter);
}catch (Exception e){
Toast.makeText(MainActivity.this,"" +e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Error:" +error,Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}