Android排球获取请求第一次无效

时间:2016-05-05 14:01:17

标签: android android-volley getjson

我想发送一个新的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课程

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

}

1 个答案:

答案 0 :(得分:1)

@seradd

你解释错了。

它实际上只执行一次。 你在调试模式中看到的是,

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

  

所以我建议你,无论你在添加任务后做了什么任务   RequestQueue调用将该代码添加到onResponse()方法