发送Base64时,Retrofit内容长度收到0错误

时间:2014-04-07 07:16:58

标签: android base64 square retrofit

我正在尝试使用form-data将String和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网址如下: http://localhost/api/reports?auth_token=""

它接受两个参数作为form-data

  • 报告类型
  • 图像

这是我的完整代码:

ReportService的

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

1 个答案:

答案 0 :(得分:1)

异常说有些东西正在设置一个请求标头,承诺一个29149字节的请求体,但不提供该体。如果你的身体应该是29149字节,你需要找出它没有被写入的原因。如果您的身体应该是空的,则需要确定Content-Length标头的来源。