Android volley get 请求在第一次时不起作用

Android volley get request does not work on first time

我想发送一个新的 JsonObjectRequest 请求 (GET)

下面是我的代码:

    final VolleyApplication volleyApplication = VolleyApplication.getInstance();
        volleyApplication.init(getApplicationContext());

        JsonArrayRequest req = new JsonArrayRequest("http://localhost:8080/webapi/", new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    VolleyLog.v("Response:%n %s", response.toString(4));
                    System.out.print(response.toString());
                    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                    Type listType = new TypeToken<List<MyEntity>>() {
                    }.getType();
                    myList = gson.fromJson(response.toString(), listType);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.e("Error: ", error.getMessage());
                System.out.print(error.getMessage());
            }
        }
        );
RequestQueue requestQueue = volleyApplication.getRequestQueue();
        requestQueue.add(req);

这适用于 onCreate 并列出一些对象,但它不起作用。正如我在调试模式下看到的那样,此方法的进程工作两次。第一次在 RequestQueue requestQueue = volleyApplication.getRequestQueue(); requestQueue.add(req);.. 行 它跳出到方法的末尾。但它有效并第二次获取数据。这搞乱了我的代码。

下面还有我的 VolleyApplication class

public final class VolleyApplication {

    private static VolleyApplication instance = null;


    public static final VolleyApplication getInstance() {
        if (instance == null) {
            instance = new VolleyApplication();
        }
        return instance;
    }

    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    private boolean initialized = false;

    private VolleyApplication() {
    }

    public void init(final Context context) {
        if (initialized) {
            return;
        }

        requestQueue = Volley.newRequestQueue(context);
        int memory = ((ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        int cacheSize = 1024 * 1024 * memory / 8;

        // imageLoader = new ImageLoader(requestQueue, new BitmapLruCache(cacheSize));
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            throw new RuntimeException("Init first");
        }
        return requestQueue;
    }

    public ImageLoader getImageLoader() {
        if (imageLoader == null) {
            throw new RuntimeException("Init first");
        }
        return imageLoader;
    }

}

@seradd

你理解错了。

实际上只执行了一次。 您在调试模式下看到的是,

第一次创建 requestObject 并将其添加到 RequestQueueRequestQueue 然后执行它,一旦它从 URL 得到响应,它将从 Response.ListenerResponse.ErrorListener 接口执行回调函数 onResponse()onErrorResponse()分别。

So what I suggest you , whatever task you are doing after adding task to RequestQueue call add that code to onResponse() method