0

I want to populate a listview from my json which looks like this:

{ library: { books: [ { name: "name", autor: "autor",year: "year"}, { name: "name", autor: "autor", year: "year"}], library: { name: "name"}}}

In main activity i have this method

public void books() {

    request = new JsonRequestClass(getContext(), JsonMethodClass.GET, null);
    try {
        String response = request.execute("http://booksjson").get();

        ResponseListModel responseListModel = new ResponseListModel(response);
        if (responseListModel.isSuccess() == true) {

        } else {
            responseListModel.setSuccess(false);
            responseListModel.setMessage("Error");
            //Toast.makeText(getContext(), responseListModel.getMessage(), Toast.LENGTH_LONG).show();
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

}

this method help me to read the json but how do i populate the listview if i have a void method and in my adapter class is waiting for an arraylist from model class?

public BookAdapter(Context context, List<Book> list) {
    this.context = context;
    this.list = list;
}

I-m adding my ResponseListModel model class

public class ResponseListModel {

    private boolean success;
    private String message;
    private JSONArray list;
    private JSONObject object;

    public ResponseListModel(String pObjetc) {
        try {

            JSONObject object = new JSONObject(pObjetc);

            this.message = object.getString("message");
            this.success = object.getBoolean("success");
            //response.setMessage(object.optString("message"));
            String strModel = object.getString("strModel");
            String type = object.optString("type");
            if (type.equals("array")) {
                this.list = object.getJSONArray("model");// new JSONArray(strModel);
            } else
                object = object.getJSONObject("model");

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public JSONArray getList() {
        return list;
    }

    public JSONObject getObject() {
        return object;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setList(JSONArray list) {
        this.list = list;
    }

    public void setObject(JSONObject object) {
        this.object = object;
    }
}

And to be more specific i need help on this line on my main

adapter = new BookAdapter(this, books);
Diego Magdaleno
  • 831
  • 8
  • 20
  • You have to parse the string in `response` to an array or list of `Book`. I don't know this class `ResponseListModel` but it seems to do the job. So, probably, there is some method like `getBody()`, `getModel()` or `getResponse()` to be called from the `responseListModel` instance that should return the list/array you need. Maybe you have to do some cast too. – Diego Magdaleno Feb 19 '19 at 16:36
  • i added the responselistmodel class maybe you can give me a hand? –  Feb 19 '19 at 17:08
  • But did you create this class yourself or is it some library class? – Diego Magdaleno Feb 19 '19 at 19:19
  • @DiegoMagdaleno i created it –  Feb 19 '19 at 19:22
  • So, if you wanna keep this strategy, you should now implement a json parser to convert your string in objects. Take a look at this tutorial: https://www.tutorialspoint.com/android/android_json_parser.htm - But I should say that this is not best way to do that. There are plenty of good parsers available like jackson or gson. – Diego Magdaleno Feb 19 '19 at 20:19
  • the thing is, i dont know how to do it with my json structure, i saw a lot examples with differents structures than this one and whenever i try to adapt it doesnt works –  Feb 19 '19 at 20:44
  • If you want to write your own parser, you got to make sure the json structure is the one you are expecting for. You can achieve this by printing your `response` string. After that, you can see if it is right to parse your array from `model` in the `ResponseListModel` class. Again, here is a simple example using jackson: https://www.codexpedia.com/java/jackson-parser-example-in-android/ – Diego Magdaleno Feb 20 '19 at 19:51

2 Answers2

0

You have to deserialize your json array response to arraylist of java object. You can use default Json android json package or gson library to do that.

Then you can feed your adapter with that arraylist of your Book class objects.

For more details you can refer to json to recyclerview.

FYI: You should use updated version of listview which is Recyclerview

Happy coding......:)

rawcoder064
  • 1,374
  • 3
  • 11
  • 26
0

You have to convert that response variable into a java class, you can use clases like Gson and annotations.

You may check this page and paste your JSON to help you understand its structure.

Davis
  • 137
  • 1
  • 6