Iam Creating an android application to read json values webservice. The service returning following json response.
[{"pk_int_kv_category_id":1,"vchr_kv_category_name":"Parent","fk_int_kv_category_id":1,"vchr_delete_status":"s","vchr_last_modified_by":1,"vchr_last_modified_time":1},{"pk_int_kv_category_id":2,"vchr_kv_category_name":"Events","fk_int_kv_category_id":1,"vchr_delete_status":"s","vchr_last_modified_by":123,"vchr_last_modified_time":"2017-08-23 14:46:04"}]
And the following section of my program fetch the json values from url
public void makeJSONRequest(String categoryurl, String JSON_ARRAY, final String pk_int_kv_category_id_obj, final String vchr_kv_category_name_obj, final String fk_int_kv_category_id_obj, final String vchr_delete_status_obj, final String vchr_last_modified_by_obj, final String vchr_last_modified_time_obj) {
//listAdapter.notifyDataSetChanged();
//newsItems.clear();
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading Latest News...");
pDialog.show();
//JSONObject jsonObject = null;
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
categoryurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
//Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_LONG).show();
if (response != null) {
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
parseJsonFeed(response, pk_int_kv_category_id_obj, vchr_kv_category_name_obj, fk_int_kv_category_id_obj, vchr_delete_status_obj, vchr_last_modified_by_obj, vchr_last_modified_time_obj);
hidePDialog();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding our request to the queue
requestQueue.add(jsonReq);
}
protected void parseJsonFeed(JSONObject response, String pk_int_kv_category_id, String vchr_kv_category_name, String fk_int_kv_category_id, String vchr_delete_status, String vchr_last_modified_by, String vchr_last_modified_time) {
try {
JSONArray categoey_array = response.getJSONArray(JSON_ARRAY);
for(int i=0;i<categoey_array.length();i++) {
JSONObject feedobj=(JSONObject) categoey_array.get(i);
pk_int_kv_category_id1 = feedobj.getString(pk_int_kv_category_id);
vchr_kv_category_name1 = feedobj.getString(vchr_kv_category_name);
fk_int_kv_category_id1 = feedobj.getString(fk_int_kv_category_id);
vchr_delete_status1 = feedobj.getString(vchr_delete_status);
vchr_last_modified_by1 = feedobj.getString(vchr_last_modified_by);
vchr_last_modified_time1 = feedobj.getString(vchr_last_modified_time);
Toast.makeText(this,vchr_kv_category_name.toString(),Toast.LENGTH_LONG).show();
}
}catch (JSONException jsonexception){
Toast.makeText(this,jsonexception.toString(),Toast.LENGTH_LONG).show();
}
}
But While running my application i'm getting the following error
value [{"pk_int_kv_category_id":1,"vchr_kv_category_name":"Parent","fk_int_kv_category_id":1,"vchr_delete_status":"s","vchr_last_modified_by":1,"vchr_last_modified_time":1},{"pk_int_kv_category_id":2,"vchr_kv_category_name":"Events","fk_int_kv_category_id":1,"vchr_delete_status":"s","vchr_last_modified_by":123,"vchr_last_modified_time":"2017-08-23 14:46:04"}] of type org.json.JSONArray cannot be converted to JSONObject
change this lines
to