列表没有得到价值

时间:2017-10-27 18:59:16

标签: android for-loop arraylist

我有一个互联网请求,向我的应用程序发送一个JSON对象列表(请求正常工作,我已经尝试过了)。当我尝试将所有json保存到与JSON相同的Object的List时,它会全部为null,我不知道如何解决这个问题。

以下是for中保存的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

    }

这是我获取数据的地方:

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);

                        }
                    });
                }
            });


        }

这是在调试器中看到的结果:

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+"]";
}
}

站长:

    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+"]";
}
}

1 个答案:

答案 0 :(得分:1)

问题出在你的构造函数中。 你应该在那里初始化你的实例:

(defun rslots-get (o l)
  (if (cdr l)
      (rslots-get (slot-value o (car l)) (cdr l))
      (slot-value o (car l))))

(defun rslots-set (o l z)
  (setf
   (slot-value o (car l))
   (if (cdr l)
       (rslots-set (slot-value o (car l)) (cdr l) z)
       z))
  o)

(defun rslots-push (o l z)
  (setf
   (slot-value o (car l))
   (if (cdr l)
       (rslots-push (slot-value o (car l)) (cdr l) z)
       (push z (slot-value o (car l)))))
  o)

(defmacro ?? (o   &rest l) `(rslots-get  ,o ',l   ))
(defmacro !! (o z &rest l) `(rslots-set  ,o ',l ,z))
(defmacro << (o z &rest l) `(rslots-push ,o ',l ,z))