0

I'm really slow and I can't grasp how I can initialize that I am going into object then into an array. I've tried making a new Gson from the Components, but I don't know how. Should I make a new Class inside of the model, thats also an Array? And then do <ArrayList<Component.Components(inside class)>> ?

Model

public class Component {
    @SerializedName("total")
    transient private Integer total;
    @SerializedName("rows")
    private ArrayList<Rows> rows;

    public class Rows {
        @SerializedName("name")
        private String name;
        @SerializedName("image")
        private String image;
        @SerializedName("serial")
        private String serial;
        @SerializedName("purchase_cost")
        private String cost;

        public Rows(String name, String image, String serial, String cost) {
            this.name = name;
            this.image = image;
            this.serial = serial;
            this.cost = cost;
        }

   //getters
    }

}

API

   @GET("api/v1/components")
   Call<ArrayList<Component.Rows>> listComponents();

MainActivity

     JsonPlaceHolderApi jsonPlaceHolderApi = ApiClient.getClient().create(JsonPlaceHolderApi.class);
        Call<ArrayList<Component.Rows>> call = jsonPlaceHolderApi.listComponents();

        call.enqueue(new Callback<ArrayList<Component.Rows>>() {
            @Override
            public void onResponse(@NonNull Call<ArrayList<Component.Rows>> call, @NonNull Response<ArrayList<Component.Rows>> response) {

                ArrayList<Component.Rows> posts = response.body();
                componentAdapter = new ComponentAdapter(getApplicationContext(),posts);
                listView.setAdapter(componentAdapter);
            }

JSON:

{
    "total": 1,
    "rows": [
        {
            "id": 1,
            "name": "HP ENVY x360 - 13-ag0017nn - 4UE32EA AMD® Raven Ridge Ryzen 7 2700U do 3.8GHz, 13.3&quot;, 512GB SSD, 8GB",
            "image": "http://server/uploads/components/7cDXBttwk2O5p5sEM5T9raBvW.png",
            "serial": "193015227095",
            "location": {
                "id": 1,
                "name": "ICB"
            }, 

This is the response json.

2 Answers2

0

I am not familiar with your code but I hope my answer helps you ..you can get ArrayList from an array by using class Arrays as

Arrays.asList(myArray);

it returns a list of items in myArray it also accepts var parameter you can use it in one line like

List<Integer> l = new ArrayList<>(asList(5,4,4,4));

or

Integer[]arr = new Integer[5];
    List<Integer> l = new ArrayList<>(asList(arr));
Noah Mohamed
  • 114
  • 1
  • 1
  • 8
0

As per your response json

{
    "total": 1,
    "rows": [
        {
            "id": 1,
            "name": "HP ENVY x360.....",
            "image": "http://server/uploads/components/7cDXBttwk2O5p5sEM5T9raBvW.png",
            "serial": "193015227095",
            "location": {
                "id": 1,
                "name": "ICB"
            }
        },
        .. similar objects ...
    ]
}

Your received class structure should be like

class ResponseData {
    private int total;
    private List<RowData> rows;
    // todo: getters & setters

    public class RowData {
        private int id;
        private String name;
        // etc etc
        private LocationData location;

        public class LocationData {
            private int id;
            private String name;
            // etc etc
        }
    }
}

And then, Your API and activity becomes

// API
Call<ResponseData> listComponents();

// activity code
Call<ResponseData> call = jsonPlaceHolderApi.listComponents();

// reading rows
ResponseData response = response.body();
response.getRows();

Hope you understand it.

Monster Brain
  • 1,950
  • 18
  • 28
  • Your'e trying to get json from server right ? where's the error occuring ? – Monster Brain May 04 '19 at 12:22
  • com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ This happens when you don't store the json properly. My code is only good for objects but I dont know how to accept an array. – Milan Ciganović May 04 '19 at 12:35
  • MalformedJsonException error occurs due to your response having, may be unknown characters. You can refer this [stackoverflow answer](https://stackoverflow.com/questions/39918814/use-jsonreader-setlenienttrue-to-accept-malformed-json-at-line-1-column-1-path) – Monster Brain May 04 '19 at 12:39
  • Had this issue before, this doesn't fix it. It will say expected BEGIN_ARRAY found BEGIN_OBJECT. – Milan Ciganović May 04 '19 at 12:45