I am trying to parse the following JSON data into a recycler view. In particular, I want to parse the address object to display the locationName, line1, city, state and zip, but I cant get the parsing method right.
{
"pollingLocations": [
{
"address": {
"locationName": "LITTLE LEAGUE HEADQUARTERS",
"line1": "6707 LITTLE LEAGUE DR",
"city": "SAN BERNARDINO",
"state": "CA",
"zip": "92407"
},
"notes": "",
"pollingHours": "07:00-20:00",
"sources": [
{
"name": "Voting Information Project",
"official": true
}
]
}
]
}
My issue is finding the address object. With the current code, I get a logcat error that says No value for locationName. I think this is because I first need to specify the address object to iterate through, but how do I do this? This is the current implementation that I have right now.
//fetch
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("pollingLocations");
for(int i = 0; i < array.length(); i++){
JSONObject addressObject = array.getJSONObject(i);
for (int x = 0; x < addressObject.length(); x++){
VotingLoc votingLoc = new VotingLoc(addressObject.getString("locationName"),
addressObject.getString("line1"),
addressObject.getString("city"),
addressObject.getString("state"),
addressObject.getString("zip"),
addressObject.getString("pollingHours"));
votingList.add(votingLoc);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
Is there an easier way of doing this? I believe the code for my rvadapter class and is correct and the error is in this method, but If you want to see any other code, let me know.

address