Volley JsonObjectRequest响应不按顺序排列

时间:2015-11-10 07:13:39

标签: android android-volley android-json

当我尝试在队列中添加多个JsonObjectRequest时,我将其响应响应添加到数组列表中,现在问题是响应的顺序不同,我提出请求它随机存储在数组列表中。所以我无法识别网址的响应,因为它是随机的。

实施例

请求[1,2,3,4]

响应[2,1,4,3]

任何解决方案?提前谢谢。

MainClass

public class Maker extends Fragment implements Response.Listener, Response.ErrorListener {

    private String[] urls = {Declaration.Url1, Declaration.Url2, Declaration.Url3};
    private ArrayList<String> mResponces = new ArrayList<>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        for (String url : urls) {
            JsonObjectRequest request = new JsonObjectRequest(url, null, this, this);
            AppController.getInstance().addToRequestQueue(request);
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("onErrorResponse", error.toString());
    }

    @Override
    public void onResponse(Object response) {
        mResponces.add(response.toString().trim());
        if (mResponces != null && mResponces.size() == urls.length) {
            PopulateData();
        }
    }

    private void PopulateData() {
        // The problem is the response are not in order
        String ResponceFormUrl1 = mResponces.get(0);
        String ResponceFormUrl2 = mResponces.get(1);
        String ResponceFormUrl3 = mResponces.get(2);
    }
}

AppController的

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

0 个答案:

没有答案