I am very new to this, so I have no idea how to do it and I'm still in the process of learning how to parse JSON files. I am trying to parse this JSON Object using Volley, but I am missing the logic on what to do next. Thank you! Here's my code:
public class MainActivity extends AppCompatActivity {
private TextView results;
String JsonUrl = "http://api.androidhive.info/contacts/";
String data = "";
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(this);
results = (TextView) findViewById(R.id.jsonData);
JsonObjectRequest objRequest = new JsonObjectRequest(Request.Method.GET,
JsonUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject contactObj = response.getJSONObject("contacts");
for (int i = 0; i < contactObj.length(); i++) {
String id = contactObj.getString("id");
String name = contactObj.getString("name");
data += "";
data += "Id " + id + "\n\n";
data += "Name " + name + "\n\n";
}
results.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
});
requestQueue.add(objRequest);
}
}