我使用 web服务调用了Retrofit2,但是onResponse没有被调用,onFailure被称为(尽管有),服务的操作在服务器端上完全成功--
每当我尝试使用fiddler来检查服务的工作情况时,我发现问题是序列化服务的后续响应,当使用fiddler时,我发现JSON响应中没有任何内容,所以Retrofit服务假设它失败了,因为没有内容,它不能序列化空的内容并给我这个错误。
java.io.EOFException: End of input at line 1 column 1Fiddler原始响应
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: nosniffFiddler Json响应为空
java中的java服务
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 .但失败了
webservice的接口
@PATCH("alerts/{alert_id}/accept")
Call<Object> accept_invited_alerts(@Header("X-Api-Token") String
api_token, @Path("alert_id") int alert_id);发布于 2016-03-02 11:02:02
如果身体是空的,请返回空值。
@PATCH("alerts/{alert_id}/accept") Call<Void> accept_invited_alerts(@Header("X-Api-Token") String api_token, @Path("alert_id") int alert_id);在使用Rx java进行改造时,您可以使用这样的方法
@PATCH("alerts/{alert_id}/accept") Observable<Response<Void>> accept_invited_alerts(@Header("X-Api-Token") String api_token, @Path("alert_id") int alert_id);编辑:为kotlin
@PATCH("alerts/{alert_id}/accept")
fun accept_invited_alerts(@Header("X-Api-Token") api_token: String, @Path("alert_id") alert_id: Int): Call<Unit>和
@PATCH("alerts/{alert_id}/accept")
fun accept_invited_alerts(@Header("X-Api-Token") api_token: String, @Path("alert_id") alert_id: Int): Observable<Response<Unit>>发布于 2018-05-18 07:26:15
您可以创建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);
}
};
}
}并添加到代码创建中。前任:
UploadImageNghiemThuApi uploadService = new Retrofit.Builder()
.baseUrl(Config.URL+"/")
.client(okHttpClient)
// -----add here-------
.addConverterFactory(new NullOnEmptyConverterFactory())
//---------------------
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(UploadImageNghiemThuApi.class);我希望它能帮助你的问题。谢谢!
编辑!
对于kotlin usecase,您可以检查这个
class NullOnEmptyConverterFactory : Converter.Factory() {
override fun responseBodyConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *> {
val delegate: Converter<ResponseBody, *> =
retrofit.nextResponseBodyConverter<Any>(this, type, annotations)
return Converter { body -> if (body.contentLength() == 0L) null else delegate.convert(body) }
}}
发布于 2017-07-22 17:23:45
非常感谢。
Api接口
@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());https://stackoverflow.com/questions/35744795
复制相似问题