【问题标题】:Volley Request - No Response if URL is wrong, How to detect this scenario in volley request?排球请求 - 如果 URL 错误,则无响应,如何在排球请求中检测到这种情况?
【发布时间】:2020-12-17 14:54:47
【问题描述】:

您好,我正在尝试从 JSON 返回的 URL 下载一些图像,但如果此类请求 URL 错误,我不会从 volley 请求中得到任何响应,并且会卡在那里很长时间。我在下面包含了一个示例代码块。 FetchImage 方法在循环中被调用,但是从 URL 错误的请求中,onResponse 或 onError 块中没有响应,我无法检测到进程是否已经结束。

    final RequestQueue queue = Volley.newRequestQueue(this);
progressbar.setVisibility(View.VISIBLE);
String url = Constants.API_BASE_URL

FetchImage(url, queue);

private void FetchImage(String url, RequestQueue queue) {
    //get image file name
    final String imageFile = utils.getFileNameFromPath(url);

    final File imgDIR = new File(systemCardSavePath);
    if(!imgDIR.exists()){
        imgDIR.mkdirs();
    }

    ImageRequest imageRequest = new ImageRequest(url,
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    try{
                        File file = new File(imgDIR, imageFile);
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();
                        FileOutputStream fileoutputstream = new FileOutputStream(file);
                        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
                        response.compress(Bitmap.CompressFormat.PNG, 100, bytearrayoutputstream);
                        fileoutputstream.write(bytearrayoutputstream.toByteArray());
                        fileoutputstream.close();
                        cardData.put(imageFile, "1");

                        requestsCounter.decrementAndGet();
                        if (requestsCounter.get() == 0) {
                            progressbar.setVisibility(View.INVISIBLE);
            }

                    }catch (FileNotFoundException exFNFE){
                        exFNFE.printStackTrace();
                    }catch (IOException exIO){
                        exIO.printStackTrace();
                    }
                }
            }, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //do error handling
        }
    });

    queue.add(imageRequest);
    requestsCounter.incrementAndGet();
}

【问题讨论】:

    标签: android request android-volley


    【解决方案1】:

    如果 url 错误或不正确的 url 格式甚至不存在,那么我们将得到 BadUrl Exception。然后通过传递一些消息来处理错误。提供正确的网址是我们的责任。这是开发商的错误。这与用户无关。

    【讨论】:

      【解决方案2】:

      为了处理不同类型的错误,我们需要对 volley 进行适当的错误处理,如下所示(错误处理的顺序也很重要):

      new Response.ErrorListener() {
                          @Override
                          public void onErrorResponse(VolleyError error) {
                              if (error instanceof TimeoutError) {
                              } else if(error instanceof ClientError) {
          //404 error can be further traced under ClientError section
                               if(((NetworkResponse) ((ClientError)error).networkResponse).statusCode == 404){
                               }
                              } else if (error instanceof ServerError) {
                              } else if (error instanceof AuthFailureError) {
                              } else if (error instanceof NetworkError) {
                              } else if (error instanceof ParseError) {
                              } else if (error instanceof NoConnectionError) {
                              }
                          }
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        相关资源
        最近更新 更多