In the process of using retrofit
, usually, if the server returns one of our response
.
As large as the following
{
"code":200,
"msg":"success",
"result":"result"
}
In result
is the data we need to request, so when writing apis
we can write
Call<Response<Result>> getResult(Requset request);
The Result
here is obtained according to actual needs. If this is the case, it will be relatively simple.
But if the value of the result
field returned by the server is encrypted, then it cannot be parsed directly at this time. The method I can think of now is to add an interceptor to okhttp
, directly modify the fields in the returned data (but I don’t know how to do it yet). Now I feel that my idea is not very good. I would like to ask if there is any way to solve it better. Customize a ConverterFactory
?
Thank you, I think this situation should not be uncommon, so I would like to ask some experts. Thank you
RetrofitCustom data protocol steps:
Customized
Converter.Factory
, refer toretrofit2.converter.gson.GsonConverterFactory
Call
Retrofit.Builder#addConverterFactory(Converter.Factory)
In addition, the calling method can also be customized. The most popular one currently is Retrofit+RxJava(RxAndroid). The steps are as follows:
Customized
CallAdapter.Factory
, refer toretrofit2.adapter.rxjava.RxJavaCallAdapterFactory
Call
Retrofit.Builder#addCallAdapterFactory(CallAdapter.Factory)
Generally, the generic T is used in Result to represent the entity actually returned from the server. As you said, you can first use the interceptor to decode the result, and then directly parse it with gson