Volley ImageLoader为某些https网址提供403响应

时间:2015-09-12 05:58:27

标签: android https android-volley http-status-code-403

我在我的项目中使用Volley Library

我正在使用ImageLoader加载&显示来自https网址的图片。

例如:

ImageLoader mImageLoader;
ImageView mImageView;
// The URL for the image that is being loaded.
private static final String IMAGE_URL =
    "https://developer.android.com/images/training/system-ui.png";
...
mImageView = (ImageView) findViewById(R.id.regularImageView);

// Get the ImageLoader through your singleton class.
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
         R.drawable.def_image, R.drawable.err_image));

它成功加载了一些图像但并非全部加载。

对于某些图片,我得到以下错误回复:

BasicNetwork.performRequest: Unexpected response code 403 for https://developer.android.com/images/training/system-ui.png

解决此问题的任何解决方案?

3 个答案:

答案 0 :(得分:0)

我已经尝试过你的代码,但它似乎对我来说很好。

发送ImageRequest的方法:

 private void sendImageRequest() {
        final String IMAGE_URL =
                "https://developer.android.com/images/training/system-ui.png";
        ImageRequest request = new ImageRequest(IMAGE_URL,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        Log.e("Image Sucess", bitmap.toString());
//                        mImageView.setImageBitmap(bitmap);
                        imageView.setImageBitmap(bitmap);
                    }
                }, 0, 0, null,
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Image Error", error.getMessage());
//                        mImageView.setImageResource(R.drawable.image_load_error);
                    }
                });
// Access the RequestQueue through your singleton class.
        Volley.newRequestQueue(getApplicationContext()).add(request);
    }
gradle中的

库:

  compile 'com.mcxiaoke.volley:library:1.0.19'

Manifest.xml中的Internet权限

 <uses-permission android:name="android.permission.INTERNET" />

请尝试使用此代码。

感谢。

答案 1 :(得分:0)

与您在上述问题中的评论一起,您可以在BasicNetwork.java中找到更多信息:

...
               } catch (IOException e) {
                    int statusCode = 0;
                    NetworkResponse networkResponse = null;
                    if (httpResponse != null) {
                        statusCode = httpResponse.getStatusLine().getStatusCode();
                    } else {
                        throw new NoConnectionError(e);
                    }
                    VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
                    if (responseContents != null) {
                        networkResponse = new NetworkResponse(statusCode, responseContents,
                                responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
                        if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
                                statusCode == HttpStatus.SC_FORBIDDEN) {
                            attemptRetryOnException("auth",
                                    request, new AuthFailureError(networkResponse));
                        } else {
                            // TODO: Only throw ServerError for 5xx status codes.
                            throw new ServerError(networkResponse);
                        }
                    } else {
                        throw new NetworkError(networkResponse);
                    }
                }

如果回复代码为 403 - 禁止,则会调用attemptRetryOnException(),您将获得该图片。

希望这有帮助!

答案 2 :(得分:0)

不是VolleyImageLoader错误。

其实我正在尝试直接使用存储桶网址访问亚马逊s3存储桶的图像。

虽然该网址上不存在图片,但当用户无权访问该网址时,S3会返回403而不是404。

Solved by this answer, check it for more details