在onResponse里面的空ArrayList

时间:2018-06-19 06:27:47

标签: java android arraylist android-volley

我正在开发一个用于从Flickr下载图像及其描述的应用程序。

我将描述字符串设置为GetterAndSetter类的ArrayList,当我想从该类中获取数据时,

甚至记录器也不打印。因此,当我尝试将stringArray中的值设置到UI上时,我得到ArrayIndexoutofBound Exception

我的代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
     LoadImages(this);
  ...
}

我的LoadImages方法:

 private void LoadImages(MainActivity mainActivity) {
    if (checkInternet()) {
        StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("Respone", "onResponse: " + response);

                //  Used to Get List of Images URLs
                getResponse = ParseJSON(response);

                List<String> urlList = getResponse.get(0);
                List<String> titles = getResponse.get(1);

                // Getting String Array from GetterAndSetter Class...
                // Not Properly Getting from there...
                List<String> Details = getterAndSetter.getStringList();

                // Printing Check
                for (String urls:urlList) {
                    Log.d("urls", urls);
                }

                // Printing Check
                for (String title:titles) {
                    Log.d("titles", title);
                }


                // Problem is here, Not Getting 
                **This is not even Executing...**
                for (String str: Details) {
                    Log.d("GetDetails", str);
                }
                ...                    
            }
        }, error -> {
            Log.d(TAG, "onErrorResponse: Error Occured...");
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        mView.show(getSupportFragmentManager(), "Loading...");
    } else {
        Toast.makeText(this, "Turn Internet on...", Toast.LENGTH_SHORT).show();
    }
}

我的ParseJSON方法:

ArrayList<ArrayList<String>> ParseJSON(String URL) {
    try {
        ...
        ArrayList<String> listURLS = new ArrayList<>();
        ArrayList<String> Titles = new ArrayList<>();

        ArrayList<ArrayList<String>> result = new ArrayList<>();

        for (int i = 0; i < photo.length(); i++) {

            JSONObject photosJSONObject = photo.getJSONObject(i);
            String FarmID = photosJSONObject.getString("farm");
            String ServerID = photosJSONObject.getString("server");
            String ID = photosJSONObject.getString("id");
            String SecretID = photosJSONObject.getString("secret");
            String ImageTitle = photosJSONObject.getString("title");

            listURLS.add(i, CreatePhotoURL(FarmID, ServerID, ID, SecretID));
            Titles.add(i, ImageTitle);

            String CreateURL = "https://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=" + API_Key + "&photo_id=" + ID + "&format=json&nojsoncallback=1";

            AddtoList(CreateURL);

            result.add(listURLS);
            result.add(Titles);
        }
        return result;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

我的AddtoList方法:

public void AddtoList(String CreateURL) {
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    StringRequest stringRequest = new StringRequest(CreateURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("Desc", response);
            JSONObject root = null;
            try {
                root = new JSONObject(response);
                JSONObject photo = root.getJSONObject("photo");

                String username = photo.getJSONObject("owner").getString("username");
                String DateTaken = photo.getJSONObject("dates").getString("taken");
                String Views = photo.getString("views");

                String FormattedStr = "Date Taken : " + DateTaken + "\n" + "Views : " + Views + "\n" + "User Name : " + username + "\n";

                // Properly Working... 
                Log.d("Describe", FormattedStr);

                // I cant say about this...
                getterAndSetter.getStringList().add(FormattedStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "onErrorResponse: " + error.toString());
        }
    });
    requestQueue.add(stringRequest);
}

我的GetterAndSetter类:

public final class GetterAndSetter {

private final ArrayList<String> stringList = new ArrayList<>();

public ArrayList<String> getStringList() {
    return stringList;
}}

0 个答案:

没有答案