小编Vin*_*yen的帖子

Android Retrofit 2,addInterceptor和addNetworkInterceptor之间的差异用于编辑响应

我一直在尝试实现拦截器(OkHttp 3.2和Retrofit 2),以便在作为响应返回之前编辑JSON响应.我们请求数据的服务器返回成功或错误的不同数据依赖性,这使得难以映射对象.

我试图通过将拦截器添加到Retrofit作为NetworkInterceptor来实现,但返回的字符串没有格式.

@Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        Response response = chain.proceed(request);
        try {

            final String responseString = new String(response.body().bytes() ); 

            LOGD("OkHttp-NET-Interceptor", "Response: " + responseString);

            String  newResponseString = editResponse( responseString );

            LOGD("OkHttp-NET-Interceptor", "Response edited: " + newResponseString);
            return  response.newBuilder()
                    .body(ResponseBody.create(response.body().contentType(), newResponseString))
                    .build();

        }catch (Exception ex){
            return response;
        }
    }
Run Code Online (Sandbox Code Playgroud)

responseString有一个没有任何可理解格式的字符串.

在更改为普通拦截器之后,字符串具有能够转换为JSONObject的格式.

可以告诉我某些回复之间的差异吗?

为什么这行新的String(response.body().bytes()); 返回不同的内容?

android interceptor retrofit2 okhttp3

23
推荐指数
1
解决办法
9801
查看次数

Android Gradle androidTestApi和testApi配置已过时

我有2个模块,模块A和模块B。模块B依赖于模块A,模块A通过使用api配置与模块B共享依赖库。

在模块A内设置测试环境时,我还使用testApiandroidTestApi使用共享的测试库来制作模块B。但是,在运行gradle sync之后,我收到了警告消息:WARNING: Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'

阅读提供的链接,并说other modules can't depend on androidTest, you get the following warning if you use the androidTestApi configuration。因此,我必须在示例中在模块B中定义测试库,以跳过此警告。

我对此情况有一些疑问:

  1. 为什么一个模块尽管可以依赖于定义为的正常依赖关系,却不应该依赖于测试另一个模块的依赖关系api
  2. 无论如何,我们是否有强迫模块B依赖模块A的测试库而不在模块B中再次定义这些库的问题?

非常感谢

testing android android-gradle-plugin

9
推荐指数
2
解决办法
1189
查看次数

在API级别24中不推荐使用DatePicker上的setSpinnersShown和setCalendarViewShown

我试图通过以编程方式更改和值来切换到DatePicker中的日历到微调器模式,但在API 24中,Android Studio显示警告.setSpinnersShownsetCalendarViewShowndeprecated

如果没有API API中的XML设置值,我可以通过编程方式将DatePicker从日历设置为spinner模式的替代方法是什么.谢谢.

mobile android datepicker datepickerdialog

7
推荐指数
1
解决办法
1993
查看次数