Reputation: 149
This is my code and I am having error to parse data from object, what changes can be done so as to parse data...
aQuery.ajax(fetch_url, JSONObject.class, new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject obj, AjaxStatus status){
super.callback(url, obj, status);
Log.i("response", url + "response:" + obj);
ArrayList<UserInfo> list = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = jsonObject.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
UserInfo info = new UserInfo();
info.id = object.getString("Id");
info.servicename = object.getString("name");
info.amount = object.getString("amount");
list.add(info);
});
}
And this is my JSON data format
{
"org_list": [
{
"Id": "1",
"name": "CBC-Test",
"amount": "200"
}
]
}
When i edit from the below code and now i am facing null response value. I have also attached my logcat image file for further more details about my problem:Click here
Click here for further more details in my code
Upvotes: 0
Views: 80
Reputation: 149
Finally I searched my self and found a solution. But it is without using AQuery and it is by using RequestQueue...
txtview = findViewById(R.id.showdata);
Button buttonParse = findViewById(R.id.showbtn);
requestQueue = Volley.newRequestQueue(this);
buttonParse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
jsonParse();
}
});
}
public void jsonParse(){
String fetchurl = "http://use your url here.com/";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, fetchurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("org_list");
for (int i=0; i < jsonArray.length(); i++){
JSONObject patient = jsonArray.getJSONObject(i);
String firstName = patient.getString("orga_organame");
txtview.append(firstName+","+"\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}
Upvotes: 0
Reputation: 1529
Edit your code as below,
aQuery.ajax(fetch_url, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject obj, AjaxStatus status) {
super.callback(url, obj, status);
Log.i("response", url + "response:" + obj);
ArrayList<UserInfo> list = new ArrayList<>();
try {
JSONArray jsonArray = obj.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
UserInfo info = new UserInfo();
info.id = object.getString("Id");
info.servicename = object.getString("name");
info.amount = object.getString("amount");
list.add(info);
}
} catch (Exception e){
e.printStackTrace();
}
}
});
No need to re-create object of JsonObject
. Just use that from response.
Upvotes: 0