如何使用Retrofit 2处理空响应体?

时间:2015-10-20 04:39:20

标签: android retrofit retrofit2 square

最近我开始使用Retrofit 2,我遇到了解析空响应体的问题。我有一个服务器只响应http代码而没有响应体内的任何内容。 如何只处理有关服务器响应的元信息(标题,状态代码等)?

提前致谢!

4 个答案:

答案 0 :(得分:180)

编辑:

杰克沃顿指出,

@GET("/path/to/get")
Call<Void> getMyData(/* your args here */);

是我最初的回应 -

您只需返回ResponseBody即可绕过解析响应。

@GET("/path/to/get")
Call<ResponseBody> getMyData(/* your args here */);

然后在你的电话中,

Call<ResponseBody> dataCall = myApi.getMyData();
dataCall.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Response<ResponseBody> response) {
        // use response.code, response.headers, etc.
    }

    @Override
    public void onFailure(Throwable t) {
        // handle failure
    }
});

答案 1 :(得分:23)

如果您使用的是rxjava,请使用以下内容:

@GET("/path/to/get")
Observable<Response<Void>> getMyData(/* your args here */);

答案 2 :(得分:2)

使用 kotlin,使用返回类型 Call<Void> 仍然抛出 IllegalArgumentException: Unable to create converter for retrofit2.Call<java.lang.Void>

使用 Response 而不是 Call 解决了问题

@DELETE("user/data")
suspend fun deleteUserData(): Response<Void>

答案 3 :(得分:0)

以下是我如何将它与Rx2和Retrofit2一起使用,以及PUT REST请求: 我的请求有一个json体,但只有http响应代码与空体。

Api客户:

public class ApiClient {
public static final String TAG = ApiClient.class.getSimpleName();


private DevicesEndpoint apiEndpointInterface;

public DevicesEndpoint getApiService() {


    Gson gson = new GsonBuilder()
            .setLenient()
            .create();


    OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    okHttpClientBuilder.addInterceptor(logging);

    OkHttpClient okHttpClient = okHttpClientBuilder.build();

    apiEndpointInterface = new Retrofit.Builder()
            .baseUrl(ApiContract.DEVICES_REST_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(DevicesEndpoint.class);

    return apiEndpointInterface;

}

界面:

public interface DevicesEndpoint {
 @Headers("Content-Type: application/json")
 @PUT(ApiContract.DEVICES_ENDPOINT)
 Observable<ResponseBody> sendDeviceDetails(@Body Device device);
}

然后使用它:

    private void sendDeviceId(Device device){

    ApiClient client = new ApiClient();
    DevicesEndpoint apiService = client.getApiService();
    Observable<ResponseBody> call = apiService.sendDeviceDetails(device);

    Log.i(TAG, "sendDeviceId: about to send device ID");
    call.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseBody>() {
        @Override
        public void onSubscribe(Disposable disposable) {
        }

        @Override
        public void onNext(ResponseBody body) {
            Log.i(TAG, "onNext");
        }

        @Override
        public void onError(Throwable t) {
            Log.e(TAG, "onError: ", t);

        }

        @Override
        public void onComplete() {
            Log.i(TAG, "onCompleted: sent device ID done");
        }
    });

}