{"record":{"id":"bac16cbaf727654b","repo":"didi/DoKit","slug":"received-response-with-0-content-length-header","errorCode":null,"errorMessage":"Received response with 0 content-length header.","messagePattern":"Received response with 0 content-length header\\.","errorType":"exception","errorClass":"ContentLengthException","httpStatus":null,"severity":"warning","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/NetworkRequestHandler.java","lineNumber":67,"sourceCode":"      return null;\n    }\n\n    DokitPicasso.LoadedFrom loadedFrom = response.cached ? DISK : NETWORK;\n\n    Bitmap bitmap = response.getBitmap();\n    if (bitmap != null) {\n      return new Result(bitmap, loadedFrom);\n    }\n\n    InputStream is = response.getInputStream();\n    if (is == null) {\n      return null;\n    }\n    // Sometimes response content length is zero when requests are being replayed. Haven't found\n    // root cause to this but retrying the request seems safe to do so.\n    if (loadedFrom == DISK && response.getContentLength() == 0) {\n      Utils.closeQuietly(is);\n      throw new ContentLengthException(\"Received response with 0 content-length header.\");\n    }\n    if (loadedFrom == NETWORK && response.getContentLength() > 0) {\n      stats.dispatchDownloadFinished(response.getContentLength());\n    }\n    return new Result(is, loadedFrom);\n  }\n\n  @Override int getRetryCount() {\n    return RETRY_COUNT;\n  }\n\n  @Override boolean shouldRetry(boolean airplaneMode, NetworkInfo info) {\n    return info == null || info.isConnected();\n  }\n\n  @Override boolean supportsReplay() {\n    return true;\n  }","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/NetworkRequestHandler.java#L49-L85","documentation":"NetworkRequestHandler throws ContentLengthException when a response served from the local disk cache reports a 0 content-length header. The code comments note this happens when requests are being replayed and no root cause was found; throwing lets the request pipeline retry, which resolves it. Retrying is explicitly considered safe.","triggerScenarios":"A disk-cache hit (loadedFrom == DISK) whose Response.getContentLength() returns 0; the handler closes the stream and throws so the load can be retried.","commonSituations":"OkHttp replaying a cached response with a stripped/empty body on flaky networks; conditional-cache revalidation edge cases; cache corruption after an interrupted write. Usually intermittent and self-healing on retry.","solutions":["Rely on Picasso's built-in retry: NetworkRequestHandler.shouldRetry returns true, so simply letting the request retry usually resolves it","Clear the disk cache if the error persists for the same URLs (possible partial cache entry)","Upgrade okhttp/picasso alignment if a version mismatch in cache interceptors is suspected"],"exampleFix":"// before: caller treats any exception as fatal\ntry { picasso.load(url).into(view); } catch (Exception e) { /* give up */ }\n\n// after: retry once via Picasso's retrying mechanism or a manual re-load\n// (retry is safe per the source comment; ContentLengthException is retriable)","handlingStrategy":"retry","validationCode":"// nothing to validate client-side; the condition comes from the disk cache layer\n// mitigations: keep okhttp/picasso versions aligned, clear cache when persistent","typeGuard":null,"tryCatchPattern":"// ContentLengthException extends IOException; let Picasso's shouldRetry path retry it.\n// For manual loads: retry once before surfacing an error to the UI.\nif (exception instanceof ContentLengthException) { retryLoadOnce(); }","preventionTips":["Trust the built-in retry; this condition is documented as safe to retry","Clear the disk cache if the same URL fails repeatedly (possible corrupt entry)","Keep the disk cache intact across requests; do not strip Content-Length in custom interceptors"],"tags":["android","picasso","network","cache","retry"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}