0

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

Logcat View1

Logcat View2

Ayush Katuwal
  • 139
  • 1
  • 2
  • 11

2 Answers2

0

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.

Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27
  • I copy this and edit in my code; its not happening! Yes, your code let me to remove red lines but the data is not parsing, it is showing null response in logcat. – Ayush Katuwal Dec 25 '17 at 09:46
  • r u getting something in `obj`..? – Dhruv Patel Dec 25 '17 at 09:50
  • you mean to say in logcat... no i am getting nothing in there. – Ayush Katuwal Dec 25 '17 at 10:02
  • so, problem is in calling web service. B'cos you r getting nothing in response. – Dhruv Patel Dec 25 '17 at 10:05
  • ya i also think the same but when i hit that url in postman it shows the data and i also checked the getString value name, they have same name as it is in json data. I am totally confused... Do we have something missing in code? – Ayush Katuwal Dec 25 '17 at 10:14
  • r u sure there is no errors in logcat?? and I hope you have given internet permission. – Dhruv Patel Dec 25 '17 at 10:17
  • sir i have attached my logcat image file in my question, you can view over there if I did not understand... – Ayush Katuwal Dec 25 '17 at 10:34
  • Please review your code again. B'cos as per our conversation, problem should be in url / parameters / authorization / permission. I cannot help you further – Dhruv Patel Dec 25 '17 at 10:45
  • Before this problem, I had checked parsing data and it works when i have json data in array class, but when i just bound my same data by object class it is not working now; as I show you my code and json data. I have checked Internet Permission. My same code is fetching data from another url that have array class and it is still working. Just to check whether it works or not in another activity, when it is bound by object then it generate such problems... – Ayush Katuwal Dec 25 '17 at 10:45
  • Ok sir I will check out that and I hope you had viewed my logcat errors. Thank you for your kind help sir!! – Ayush Katuwal Dec 25 '17 at 10:47
  • Would you excuse me for a while? Below try i use " } catch (JSONException je) { Toast.makeText(aQuery.getContext(), "Error Parsing Data!", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(aQuery.getContext(), "Something went wrong...", Toast.LENGTH_LONG).show(); } " And in my activity it is showing this catch toast message. Do you any idea? – Ayush Katuwal Dec 26 '17 at 07:05
  • so what? I don't understand...r u getting any exception? – Dhruv Patel Dec 26 '17 at 07:07
  • Ya I'm getting this exception. Do you know reason behind this exception? – Ayush Katuwal Dec 26 '17 at 07:09
  • If you have any idea in what case do this exception get occur then please share me, you would be so thankful... I am stucking with this problem... – Ayush Katuwal Dec 26 '17 at 07:10
  • I have edited my code. use that and share me your error logs. – Dhruv Patel Dec 26 '17 at 07:14
  • I use your code still same message... I further edited my question by adding more images – Ayush Katuwal Dec 26 '17 at 07:34
  • Good to see, you found solution. But we should know which mistake we did. That's the **learning process for the Developers**. Never give up like this. – Dhruv Patel Dec 26 '17 at 10:02
  • Sounds good, Yes I will try my best sir and Thanks a lot for being loyal with me and with my problem. – Ayush Katuwal Dec 26 '17 at 10:24
0

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);
}
Ayush Katuwal
  • 139
  • 1
  • 2
  • 11