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

后端 未结 3 1731
抹茶落季
抹茶落季 2020-12-29 18:00

I called PATCH web service using Retrofit2 but onResponse is not called and the onFailure is called Despite of

相关标签:
3条回答
  • 2020-12-29 18:11

    Thank you very much.

    Api

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

    Class:

    Call call = service.insertLogin(login.getEmail(), login.getSenha(), login.getCodCondutor());
    
    0 讨论(0)
  • 2020-12-29 18:15

    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);
    
    0 讨论(0)
  • 2020-12-29 18:35

    You can create 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;
    
    
    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);               
                }
            };
        }
    }
    

    and add to code create. For ex:

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

    I hope it can help your problem. Thanks!

    0 讨论(0)
提交回复
热议问题