0

I've got an internet request that send to my App a list of JSON object (the request work correctly, i've already tried it). When i try to save all the json to a List of same Object as the JSON, it get all null and i dont know how to fix that.

Here's the for that save in the List:

public void getCards(MasterJsonCard mjc){

        downloadedCardList = new ArrayList<>();

        for(Master card : mjc.getMaster())
            downloadedCardList.add(new Master(card.getId(),card.getExpansion(),card.getImgpath(),card.getLitness(),card.getName(),card.getDankness(),card.getRarity(),card.getValue()));
        // THIS ONLY GET ALL NULL
        new addCartRealms().execute(); //EXECUTING A THREAD FOR SOME REASON

    }

This is where i get data:

public void downloadCard(){

        if(!isOnline()) //Controllo se è presente la connessione
            Toast.makeText(mainMenu.this, "Connessione Assente", Toast.LENGTH_LONG).show();
        else {

            String news_url = "HERE_THE_URL";

            String index_string = String.valueOf(totalRealmCardNumber);

            OkHttpClient client = new OkHttpClient();

            RequestBody formBody = new FormBody.Builder()
                    .add("index", index_string)
                    .build();

            Request request = new Request.Builder()
                    .url(news_url)
                    .post(formBody)
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, final Response response) throws IOException {
                    if (!response.isSuccessful()) {
                        throw new IOException("Unexpected code " + response);
                    }

                    downloadCardResponse = response.body().string();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {

                            Gson gson = new Gson();
                            MasterJsonCard mjc = gson.fromJson(downloadCardResponse, MasterJsonCard.class);
                            getCards(mjc);

                        }
                    });
                }
            });


        }

This is result seen in the debugger:

Result of debugger

MasterJsonCard:

   public class MasterJsonCard
{
    private Master[] master;

public Master[] getMaster ()
{
    return master;
}

public void setMaster (Master[] master)
{
    this.master = master;
}

@Override
public String toString()
{
    return "ClassPojo [master = "+master+"]";
}
}

Master:

    public class Master
{
    private String id;

private String expansion;

private String imgpath;

private String litness;

private String name;

private String dankness;

private String rarity;

private String value;

public Master(String id, String expansion, String imgpath, String litness, String name, String dankness, String rarity, String value) {
}

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public String getExpansion ()
{
    return expansion;
}

public void setExpansion (String expansion)
{
    this.expansion = expansion;
}

public String getImgpath ()
{
    return imgpath;
}

public void setImgpath (String imgpath)
{
    this.imgpath = imgpath;
}

public String getLitness ()
{
    return litness;
}

public void setLitness (String litness)
{
    this.litness = litness;
}

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public String getDankness ()
{
    return dankness;
}

public void setDankness (String dankness)
{
    this.dankness = dankness;
}

public String getRarity ()
{
    return rarity;
}

public void setRarity (String rarity)
{
    this.rarity = rarity;
}

public String getValue ()
{
    return value;
}

public void setValue (String value)
{
    this.value = value;
}

@Override
public String toString()
{
    return "ClassPojo [id = "+id+", expansion = "+expansion+", imgpath = "+imgpath+", litness = "+litness+", name = "+name+", dankness = "+dankness+", rarity = "+rarity+", value = "+value+"]";
}
}
Fyruz
  • 75
  • 1
  • 20
  • Did you create correctly the getter and setter inside of Master, because Gson need it to create correctly the object. Can you give the class Master And MasterJsonCard? In my opinion, you don't need to create both, Master is enough to represent Card object. – Djory Krache Oct 27 '17 at 19:07
  • I've added the classes – Fyruz Oct 27 '17 at 19:16
  • Advice beside, you can simplify your code doing `Master[] masters = gson.fromJson(downloadCardResponse, Master[].class);` – Djory Krache Oct 27 '17 at 19:36
  • And it ok if i use this then ?? `public void getCards(Master[] masters){ downloadedCardList = new ArrayList<>(); for(Master card : masters)` – Fyruz Oct 27 '17 at 19:39
  • If you specifically need a List, you can do after an `Arrays.asList(masters)`. Else you can directly convert it in List with Gson doing [https://stackoverflow.com/a/5554296/4479402](https://stackoverflow.com/a/5554296/4479402) – Djory Krache Oct 27 '17 at 19:44

1 Answers1

1

The problem is in your constructor. you should initialize your instance there :

public Master(String id, String expansion, String imgpath, String litness, String name, String dankness, String rarity, String value) {
      this.id = id;
      this.expansion = expansion;
      this.imgpath = imgpath;
      this.litness = litness;
      this.name = name;
      this.dankness = dankness;
      this.rarity = rarity;
      this.value = value;
}
Djory Krache
  • 347
  • 4
  • 9