【问题标题】:Android volley get request does not work on first timeAndroid volley get 请求在第一次不起作用
【发布时间】:2018-01-15 21:45:10
【问题描述】:

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

}

【问题讨论】:

    标签: android android-volley getjson


    【解决方案1】:

    @seradd

    你理解错了。

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

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

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

    【讨论】:

    • @Guarav 先生,我也面临同样的问题。您能否更详细地说明这一点,您的报价:“所以我建议您,在将任务添加到 RequestQueue 后执行的任何任务都称为onResponse() 中的代码"
    • @xamMTS onResponse() 将在您从服务器获得响应后被调用,因此如果您依赖响应添加该代码,比如说更新 UI。进入onResponse() 方法
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2015-07-03
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多