【问题标题】:Retrofit content-length received 0 error while sending Base64发送 Base64 时改造内容长度收到 0 错误
【发布时间】:2014-04-07 07:16:58
【问题描述】:

我正在尝试使用表单数据将字符串和 Base64 字符串发送到服务器。但是Retrofit 一直显示这个错误:

04-07 06:44:19.366    D/Retrofit﹕ ---- ERROR http://localhost/api/reports?auth_token=....

04-07 06:44:19.366    D/Retrofit﹕ java.io.IOException: content-length promised 29149 bytes, but received 0
        at libcore.net.http.RetryableOutputStream.close(RetryableOutputStream.java:49)
        at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:818)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
        at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
        at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:90)
        at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:48)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:287)
        at retrofit.RestAdapter$RestHandler.access$500(RestAdapter.java:197)
        at retrofit.RestAdapter$RestHandler$1.obtainResponse(RestAdapter.java:243)
        at retrofit.CallbackRunnable.run(CallbackRunnable.java:38)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at retrofit.Platform$Android$2$1.run(Platform.java:134)
        at java.lang.Thread.run(Thread.java:841)
04-07 06:44:19.366    D/Retrofit:    ---- END ERROR

我的 API URL 是这样的: http://localhost/api/reports?auth_token=""

它接受两个参数作为表单数据

  • 报告类型
  • 图片

这是我的完整代码:

报告服务

public interface ReportService {
    @FormUrlEncoded
    @POST("/reports")
    void report(@Query("auth_token") String authToken, @Field("report_type") String report_type, @Field("image") String image, Callback<Report> callback);
}

报告 API

public class ReportAPI {
    public static ReportAPI mInstance;
    private Context mContext;
    private ReportService mService;

public ReportAPI(Context context) {
    this.mContext = context;

    final RestAdapter restAdapter = new RestAdapter.Builder().setServer(Constants.BASE_URL).build();
    restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
    mService = restAdapter.create(ReportService.class);
}

public static ReportAPI getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new ReportAPI(context);
    }
    return mInstance;
}

public ReportService getService() {
    return mService;
}

报告

void report() {
    String authToken = pref.getAuthToken();
    String base64String = ""; //base64 string goes here

    ReportAPI.getInstance(this).getService().report(authToken, "in", base64String, new Callback<Report>() {
        @Override
        public void success(Report report, Response response) {
            Log.e("Report", "Reported Date: " + report.report_in_date);
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e("Report", error.getResponse() + ": Error");
        }
    });
}

请求是这样的:

---> HTTP POST http://localhost/api/reports?auth_token=
04-07 06:58:14.912    D/Retrofit﹕ Content-Type: application/x-www-form-urlencoded; charset=UTF-8
04-07 06:58:14.912    D/Retrofit﹕ Content-Length: 29149
04-07 06:58:14.916    D/Retrofit﹕ report_type=in&image=...base64string_goes_here...
04-07 06:58:14.916    D/Retrofit﹕ ---> END HTTP (29149-byte body)

我花了 4 天时间,试图找出这个问题。

附注:我也尝试使用TypedStringTypedInput

【问题讨论】:

  • 更改字段中的参数是否会更改值 29149?将base64字符串更改为不同长度的“xxx”并尝试。这将确认问题是否如下所述。
  • 也可能是服务器端的问题。
  • @DeBuGGeR 当然,该值会根据数据而变化。该 api 与 PostMan 完美配合。
  • 不,我没有使用它。
  • 我记得在使用 Url 或 HTTP 客户端为 android 开发类似的问题时,这两个本地可用的 http 库。您可以尝试将 okhttp 放在路径中,这将确保使用 OKhttpClient。只是帮助你排除故障,因为我已经使用了很多改造,这个错误似乎是不可能的。

标签: android base64 square retrofit


【解决方案1】:

该异常表示某些东西正在设置一个请求标头,承诺一个 29149 字节的请求正文,但不交付该正文。如果你的 body 应该是 29149 字节,你需要弄清楚为什么它没有被写入。如果你的 body 应该是空的,你需要弄清楚 Content-Length 标头是从哪里来的。

【讨论】:

  • 是的,我正在发送 base64 字符串,因此正文应该是 29149 字节。但我不知道为什么不写。
猜你喜欢
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
相关资源
最近更新 更多