The use of Retrofit in Kotlin

foreword


The original plan was to encapsulate Retrofit so that the project can call the interface. Unfortunately, the return structure of the interface I called was too complicated. In order to complete the encapsulation, it must be connected with the actual business and background.

This anticlimactic article should lay the foundation for the following business.

text


The interface I use is the discovery bar in the eye-opening project (if you are interested, you can download it in the application market, a very good app)

GET request
http://baobab.kaiyanapp.com/api/v5/index/tab/discovery

You can see a huge json by requesting this address. Let me talk about it first according to my understanding.

Reasons for complexity:
1. The interface structure is composed of the returned json;
2. The amount of returned json is particularly large due to the first point, and the structure is very complicated at first glance;

Structure:
There is a list in the data returned by json. Each item in the list (the structure of each item is not the same, here is the key to understanding its json) corresponds to a custom control, instantiate the control according to the returned json and add it to the view .

Advantages:
The mobile terminal only needs to customize the corresponding controls. As for how to place and control the content, it is fully controlled by the background.

Complicated and complicated, it is still an interface that returns json. The regression theme encapsulates Retrofit, and the work of parsing json will be recorded separately.

before use

    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    //转换json  挂代理的小心,即使关闭代理可能也会报错。关闭代理后重启AS,估计是AS3.0的bug
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'

The main class of the request:

class Requester {

    companion object {

        private fun <T> getService(baseUrl: String, service: Class<T>): T {

            var clien = OkHttpClient.Builder()
                    //自定义拦截器用于日志输出
                    .addInterceptor(LogInterceptor())
                    .build()

            val retrofit = Retrofit.Builder().baseUrl(baseUrl)
                    //格式转换
                    .addConverterFactory(GsonConverterFactory.create())
                    //正常的retrofit返回的是call,此方法用于将call转化成RxjavaObservable或其他类型
                   .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(clien)
                    .build()
            return retrofit.create(service)
        }

        //可用于多种不同种类的请求
        fun apiService(): ApiService {
            return getService(ApiService.baseUrl, ApiService::class.java)
        }

    }

}
interface ApiService {

    companion object {
        //此类接口的基地址
        val baseUrl = "http://baobab.kaiyanapp.com/"
    }

    //请求类型 + 路由
    @GET("api/v5/index/tab/discovery")
    fun getTabDiscovery(): Call<ResponseBody>//由于json采用手动解,所以没有用泛型

}
class LogInterceptor : Interceptor {

    val tag = "Retrofit"
    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")

    override fun intercept(chain: Interceptor.Chain): Response {

        var request = chain.request()

        Logger.i(tag, format.format(Date()) + " Requeste " + "\nmethod:" + request.method() + "\nurl:" + request.url() + "\nbody:" + request.body())

        var response = chain.proceed(request)

        //response.peekBody不会关闭流
        Logger.i(tag, format.format(Date()) + " Response " + "\nsuccessful:" + response.isSuccessful + "\nbody:" + response.peekBody(1024)?.string())

        return response
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325995906&siteId=291194637