Retrofit2 error java.io.EOFException:第1行第1列的输入结束

时间:2016-03-02 10:39:10

标签: android web-services retrofit2

我使用Retrofit2调用了 PATCH 网络服务,但未调用 onResponse onFailure 被称为尽管服务器的操作在服务器端完美

成功完成

每当我尝试使用fiddler检查服务的工作时,我发现问题是序列化即将到来的服务响应,当使用fiddler时,我发现没有JSON响应的内容,所以Retrofit服务假设它失败了,因为没有内容,也无法序列化EMPTY内容 并给我这个错误

java.io.EOFException: End of input at line 1 column 1

Fiddler raw Response

HTTP/1.1 200 OK
Server: nginx/1.9.4
Date: Wed, 02 Mar 2016 09:55:55 GMT
Content-Type: application/json
Content-Length: 0
Connection: close
Status: 200 OK
X-Content-Type-Options: nosniff

Fiddler Json响应为空

java中的webservice

Call<Object> call = TimeCapp.services.accept_invited_alerts(HomeActivity.api_token, alert_id);

call.enqueue(new Callback<Object>()
{
    @Override
    public void onResponse (Call<Object> call, Response<Object> response)
    {
        if (response.isSuccess()) {
            String x = response.body();
        }
    }
    @Override
    public void onFailure (Call<Object>call, Throwable t)
    {
        String x = t.getMessage();//java.io.EOFException: End of input at line 1 column 1
    }
}

我尝试用String,JsonObject,emptyCalssBody替换对象 ....但是它失败了

网络服务的界面

@PATCH("alerts/{alert_id}/accept")
Call<Object> accept_invited_alerts(@Header("X-Api-Token") String  
api_token, @Path("alert_id") int alert_id);

3 个答案:

答案 0 :(得分:63)

just return void instead, if the body is empty

@PATCH("alerts/{alert_id}/accept") Call<Void> accept_invited_alerts(@Header("X-Api-Token") String api_token, @Path("alert_id") int alert_id);

for retrofit with Rx java you can use something like this

@PATCH("alerts/{alert_id}/accept") Observable<Response<Void>> accept_invited_alerts(@Header("X-Api-Token") String api_token, @Path("alert_id") int alert_id);

答案 1 :(得分:3)

非常感谢。

阿比

@FormUrlEncoded
@POST("/rebu/insertusuario.php")
Call<Void> insertLogin(
        @Field("email") String email,
        @Field("senha") String senha,
        @Field("codCondutor") Long codCondutor
);

类别:

Call call = service.insertLogin(login.getEmail(), login.getSenha(), login.getCodCondutor());

答案 2 :(得分:3)

您可以创建 NullOnEmptyConverterFactory.class

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by thientvse on 18/05/2018.
 */

public class NullOnEmptyConverterFactory extends Converter.Factory {

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter(this, type, annotations);
        return new Converter<ResponseBody, Object>() {
            @Override
            public Object convert(ResponseBody body) throws IOException {
                if (body.contentLength() == 0) return null;
                return delegate.convert(body);               
            }
        };
    }
}

并添加到代码创建。例如:

 UploadImageNghiemThuApi uploadService = new Retrofit.Builder()
            .baseUrl(Config.URL+"/")
            .client(okHttpClient)
            // -----add here-------
            .addConverterFactory(new NullOnEmptyConverterFactory())
            //---------------------
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(UploadImageNghiemThuApi.class);

我希望它可以帮助你解决问题。 谢谢!