{"record":{"id":"b4ccbf06966cefe9","repo":"nostra13/Android-Universal-Image-Loader","slug":"image-request-failed-with-response-code","errorCode":null,"errorMessage":"Image request failed with response code ","messagePattern":"Image request failed with response code ","errorType":"http","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/core/download/BaseImageDownloader.java","lineNumber":132,"sourceCode":"\t\tHttpURLConnection conn = createConnection(imageUri, extra);\n\n\t\tint redirectCount = 0;\n\t\twhile (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {\n\t\t\tconn = createConnection(conn.getHeaderField(\"Location\"), extra);\n\t\t\tredirectCount++;\n\t\t}\n\n\t\tInputStream imageStream;\n\t\ttry {\n\t\t\timageStream = conn.getInputStream();\n\t\t} catch (IOException e) {\n\t\t\t// Read all data to allow reuse connection (http://bit.ly/1ad35PY)\n\t\t\tIoUtils.readAndCloseStream(conn.getErrorStream());\n\t\t\tthrow e;\n\t\t}\n\t\tif (!shouldBeProcessed(conn)) {\n\t\t\tIoUtils.closeSilently(imageStream);\n\t\t\tthrow new IOException(\"Image request failed with response code \" + conn.getResponseCode());\n\t\t}\n\n\t\treturn new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());\n\t}\n\n\t/**\n\t * @param conn Opened request connection (response code is available)\n\t * @return <b>true</b> - if data from connection is correct and should be read and processed;\n\t *         <b>false</b> - if response contains irrelevant data and shouldn't be processed\n\t * @throws IOException\n\t */\n\tprotected boolean shouldBeProcessed(HttpURLConnection conn) throws IOException {\n\t\treturn conn.getResponseCode() == 200;\n\t}\n\n\t/**\n\t * Create {@linkplain HttpURLConnection HTTP connection} for incoming URL\n\t *","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/core/download/BaseImageDownloader.java#L114-L150","documentation":"BaseImageDownloader fires IOException(\"Image request failed with response code N\") when the HTTP response code is not acceptable (shouldBeProcessed returns false — by default any code outside 200-299). The error stream has already been drained to allow connection reuse, then the exception propagates to the ImageLoader pipeline where it reaches onLoadingFailed. It signals an HTTP-level failure (404, 500, 301 without follow, etc.), not a network/IO failure.","triggerScenarios":"Loading an http(s) URI whose server returns 4xx/5xx (e.g. expired CDN link returning 404, deleted photo returning 500) or a 3xx when redirects are not followed; a custom subclass of BaseImageDownloader overriding shouldBeProcessed with stricter rules; intercepted/proxied responses returning captive-portal codes.","commonSituations":"Expired or moved image URLs in production; API gateways returning 401/403 for unauthorized asset fetches; dev machines behind corporate proxies returning 502/503; servers returning 304 to a request without conditional headers.","solutions":["Verify the URL in a browser/curl — fix the source URI if the resource is gone or moved","Handle auth-required assets by adding headers via a custom ImageDownloader (getStream override) or extraForDownloader credentials","Catch the IOException in your ImageLoadingListener.onLoadingFailed and show a fallback image","If a non-2xx code is genuinely OK in your backend, subclass BaseImageDownloader and override shouldBeProcessed"],"exampleFix":"// before\nimageLoader.displayImage(photoUrl, imageView, options); // 404 -> onLoadingFailed with IOException\n\n// after\nimageLoader.displayImage(photoUrl, imageView, options, new SimpleImageLoadingListener() {\n    @Override\n    public void onLoadingFailed(String uri, View view, FailReason reason) {\n        imageView.setImageResource(R.drawable.placeholder); // graceful fallback\n    }\n});","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"imageLoader.displayImage(uri, imageView, options, new SimpleImageLoadingListener() {\n    @Override\n    public void onLoadingFailed(String uri, View view, FailReason reason) {\n        Throwable c = reason.getCause();\n        if (c instanceof IOException && c.getMessage() != null\n                && c.getMessage().startsWith(\"Image request failed with response code\")) {\n            // HTTP-level failure: fix URL or show placeholder, do NOT retry blindly\n            ((ImageView) view).setImageResource(R.drawable.error_placeholder);\n        } else {\n            ((ImageView) view).setImageResource(R.drawable.offline_placeholder);\n        }\n    }\n});","preventionTips":["Always set showImageForFailUri(...) / an onLoadingFailed fallback for remote URLs","Validate/normalize image URLs at data-entry time (reject malformed or expired links early)","For auth-protected assets, send headers via a custom downloader instead of hoping 401 never happens","Log the response code from the message in crash reporting to distinguish server errors from client errors"],"tags":["universal-image-loader","http","network","download"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}