Android 使用rxjava改型时添加响应回调

Android 使用rxjava改型时添加响应回调,android,retrofit,rx-java,Android,Retrofit,Rx Java,我使用Rxjava进行改造以获得API的响应,因为您可以看到我使用的方法,我看不到响应中会出现什么,当然我不需要,因为我提供了GsonConverter进行改造,但出于某些调试原因,我需要看到来自API的响应。我如何才能做到这一点,我需要添加什么代码 public interface ProductApiService { String END_POINT = "http://beta.site.com/index.php/restmob/"; @GET(Url.URL_PRO

我使用Rxjava进行改造以获得API的响应,因为您可以看到我使用的方法,我看不到响应中会出现什么,当然我不需要,因为我提供了GsonConverter进行改造,但出于某些调试原因,我需要看到来自API的响应。我如何才能做到这一点,我需要添加什么代码

public interface ProductApiService
{
    String END_POINT = "http://beta.site.com/index.php/restmob/";

    @GET(Url.URL_PRODUCT_API)
    Observable<Product> getProducts(@Query("some_id") String cid);

    class Creator
    {
        public static ProductApiService getProductAPIService() {
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                    .create();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ProductApiService.END_POINT)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();

            return retrofit.create(ProductApiService.class);
        }
    }
}
公共接口ProductAPI服务
{
字符串结束点=”http://beta.site.com/index.php/restmob/";
@获取(Url.Url\u产品\u API)
可观察的getProducts(@Query(“some_id”)字符串cid);
类创建者
{
公共静态ProductApiService getProductAPIService(){
Gson Gson=new GsonBuilder()
.setDateFormat(“yyyy-MM-dd'T'HH:MM:ss.SSS'Z')
.create();
改装改装=新改装.Builder()
.baseUrl(ProductApiService.END_点)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
返回改装.create(ProductApiService.class);
}
}
}

您只能在改装2后执行此操作:将返回类型更改为包含
响应

@GET(Url.URL_PRODUCT_API)
Observable<Response<Product>> getProducts(/* ...etc... */);
@GET(Url.Url\u产品\u API)
可观察的产品(/*…等….*/);

如果您想查看
onNext
中所有可能的错误(包括
IOException
,通常使用
onError
),也可以使用
Observable

您只能在改型2后执行此操作:将返回类型更改为包含
Response

@GET(Url.URL_PRODUCT_API)
Observable<Response<Product>> getProducts(/* ...etc... */);
@GET(Url.Url\u产品\u API)
可观察的产品(/*…等….*/);

如果您想查看
onNext
中所有可能的错误(包括
IOException
,它通常使用
onError
),也可以使用
Observable

Daniel Lew的方法快速且包含最少的锅炉板代码。然而,这可能会迫使您重构您的网络逻辑。由于您提到出于调试目的需要此功能,因此使用已配置的
OkHttpClient
with可能是一种侵入性较小的策略

OkHttpClient httpClient = new OkHttpClient.Builder()
    .addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request req = chain.request();
            Response resp = chain.proceed(req);
            // ... do something with response
            return resp;
        }
    })
    .build();

Retrofit retrofit = new Retrofit.Builder()
    .client(httpClient)
    .baseUrl(ProductApiService.END_POINT)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .build();

Daniel Lew的方法速度快,包含的锅炉板代码最少。然而,这可能会迫使您重构您的网络逻辑。由于您提到出于调试目的需要此功能,因此使用已配置的
OkHttpClient
with可能是一种侵入性较小的策略

OkHttpClient httpClient = new OkHttpClient.Builder()
    .addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request req = chain.request();
            Response resp = chain.proceed(req);
            // ... do something with response
            return resp;
        }
    })
    .build();

Retrofit retrofit = new Retrofit.Builder()
    .client(httpClient)
    .baseUrl(ProductApiService.END_POINT)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .build();