簡體   English   中英

截擊請求 - 如果 URL 錯誤,則無響應,如何在截擊請求中檢測這種情況?

[英]Volley Request - No Response if URL is wrong, How to detect this scenario in volley request?

您好,我正在嘗試從 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();
}

如果 url 錯誤或不正確的 url 格式甚至不存在,那么我們將得到 BadUrl Exception。 然后通過傳遞一些消息來處理錯誤。 我們有責任提供正確的網址。 這是開發人員的錯誤。 這與用戶無關。

為了處理不同類型的錯誤,我們需要對 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) {
                        }
                    }
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM