我使用Retrofit 2调用了PATCH Web服务,但未调用onResponse并且调用了onFailure,尽管服务的操作在服务器端完美成功
无论何时,我试图使用fiddler检查服务的工作,我发现问题是序列化服务的未来响应,当使用fiddler时,我发现没有JSON响应的内容,所以Retrofit服务假设它失败了,因为没有内容,它不能序列化EMPTY内容,给我这个错误
java.io.EOFException: End of input at line 1 column 1
小提琴手原始响应
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的Web服务
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);
只是返回void代替,如果主体是空的
@PATCH("alerts/{alert_id}/accept") Call<Void> accept_invited_alerts(@Header("X-Api-Token") String api_token, @Path("alert_id") int alert_id);
为了改造Rxjava你可以使用这样的东西
@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>>
您可以创建NullOnCountyConverterFactory. 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用例,您可以检查这个
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) }
}
}
非常感谢。
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());