How does retrofit handle data type resolution for encrypted fields?
伊谢尔伦
伊谢尔伦 2017-06-05 11:11:02
0
2
688

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
滿天的星座

You can consider using an interceptor for encryption and decryption and Converter.Factory for data protocol analysis.

RetrofitCustom data protocol steps:

  1. CustomizedConverter.Factory, refer to retrofit2.converter.gson.GsonConverterFactory

  2. 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:

  1. CustomizedCallAdapter.Factory, refer to retrofit2.adapter.rxjava.RxJavaCallAdapterFactory

  2. 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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template