我试图从服务器获取一些值,一切正常但是由于某种原因我收到重复的值,服务器包含3个键和值但是在我的代码中它显示了6个键和值,请建议可能导致这种情况的原因。
FirstActivity.java
public void getGifts() {
Communicator.getInstance().on("initializeRoom", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONDictionary response = (JSONDictionary) args[0];
String str = response.get("data").toString();
try {
JSONObject dataJSo = new JSONObject(str);
JSONArray giftsData = dataJSo.getJSONArray("gifts");
for (int i = 0; i < giftsData.length(); i++) {
JSONObject gifts = (JSONObject) giftsData.get(i);
giftsId = (Integer) gifts.get("id");
giftsPrice = (Integer) gifts.get("price");
giftsMinLevel = (String) gifts.get("minLevel");
giftsUrl = (String) gifts.get("urlFile");
giftsArray.add(new GiftsModel(giftsId, giftsPrice,
giftsUrl, giftsMinLevel));
}
Singleton.instance().cache = giftsArray;
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
SecondActivity.java
public class SecondActivity{
ArrayList<GiftsModel> giftsArray = Singleton.instance().cache;
//And some code
}
Singleton.java
public class Singleton {
private Singleton() {}
static Singleton obj= null;
public static Singleton instance() {
if (obj == null) {
obj = new Singleton();
}
return obj;
}
public ArrayList<GiftsModel> cache;
}