0

I have fetched JSON data from url using ION library, but I am facing problem in displaying it in a listView in android studio.

I debug it in this way:

JSONObject jsonObject = new JSONObject(String.valueOf(result));
JSONArray jsonArray = jsonObject.getJSONArray("contacts");

and here is the output for debug:

result = {JsonObject@5328} "{"id":2,"deptName":"PWD","logo":[],"contacts":[{"contactId":25,"empName":"Chill","designation":"Manager","mobile":"","landlineOffice":"23412388","landlineRes":"2334567","fax":"123445","email":"chunga@ymail.com"},{"contactId":27,"empName":"Cena","designation":"Wrestler","mobile":"98176253","landlineOffice":"2334531","landlineRes":"444568","fax":"","email":"cena@gmail.com"}]}"
result = {JsonObject@5328} "{"id":2,"deptName":"PWD","logo":[],"contacts":[{"contactId":25,"empName":"Chill","designation":"Manager","mobile":"","landlineOffice":"23412388","landlineRes":"2334567","fax":"123445","email":"chunga@ymail.com"},{"contactId":27,"empName":"Cena","designation":"Wrestler","mobile":"98176253","landlineOffice":"2334531","landlineRes":"444568","fax":"","email":"cena@gmail.com"}]}"
jsonObject = {JSONObject@5341} "{"id":2,"deptName":"PWD","logo":[],"contacts":[{"contactId":25,"empName":"Chill","designation":"Manager","mobile":"","landlineOffice":"23412388","landlineRes":"2334567","fax":"123445","email":"chunga@ymail.com"},{"contactId":27,"empName":"Cena","designation":"Wrestler","mobile":"98176253","landlineOffice":"2334531","landlineRes":"444568","fax":"","email":"cena@gmail.com"}]}"

id is an integer sent form first activity.The code for my activity is given below:

public class ContactsActivity extends AppCompatActivity {
    ListView contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);

        Intent intent = getIntent();
        contactList = findViewById(R.id.contactList);


        int dId = intent.getExtras().getInt("id");
        String Sid = new String(String.valueOf(dId));

        Context context = getApplicationContext();
        Toast.makeText(context, "" + dId, Toast.LENGTH_SHORT).show();

        try {
            getContacts(Sid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void getContacts(final String Sid) throws Exception {
        class Contacts{
            String empName;
            String designation;
            String mobile;
            String landlineOffice;
            String landlineRes;
            String fax;
            String email;

            public Contacts(String designation, String mobile, String landlineOffice, String landlineRes, String fax, String email) {
                this.designation = designation;
                this.mobile = mobile;
                this.landlineOffice = landlineOffice;
                this.landlineRes = landlineRes;
                this.fax = fax;
                this.email = email;
            }
        }



        Ion.with(this)
                .load("http://10.180.243.19:8080/api/departments/" + Sid)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        int id = Integer.parseInt(Sid);


                            try {
                             JSONObject jsonObject = new JSONObject(String.valueOf(result));
                                JSONArray jsonArray = jsonObject.getJSONArray("contacts");

                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                        }

                });



    }
}

I expect to display list of contact details of multiple employees in a listview.

Code R
  • 21
  • 4
Sam
  • 11
  • 1
  • 5
  • What exactly is your problem are you not able to display list? – p.mathew13 Apr 30 '19 at 09:49
  • I have a problem because I do not know how to extract those JSON data to array and display it in list View – Sam Apr 30 '19 at 10:04
  • You can use Gson to convert your JSON to object classes . – p.mathew13 Apr 30 '19 at 10:24
  • Kindly, can you provide link or more info. Thanks – Sam Apr 30 '19 at 10:28
  • Please refer this link https://medium.com/@ankit.sinhal/parsing-json-with-gson-library-184d94a04dec and officila GSON doc https://github.com/google/gson/blob/master/UserGuide.md .. You can use http://www.jsonschema2pojo.org/ to convert your JSOn to java objects – p.mathew13 Apr 30 '19 at 10:52

0 Answers0