1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cn.flightfeather.thirdapp.common.net
 
import cn.flightfeather.thirdapp.BuildConfig
import cn.flightfeather.thirdapp.CommonApplication
import com.google.gson.GsonBuilder
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
 
/**
 * @author riku
 * Date: 2019/7/22
 * 网络请求
 */
class RetrofitFactory constructor(val application: CommonApplication){
 
    val retrofit: Retrofit = application.retrofit
    val retrofitImage: Retrofit = application.retrofitImage
 
    private fun newOkHttpClient(list: List<Interceptor>): OkHttpClient =
            OkHttpClient.Builder()
                    .connectTimeout(20 * 1000L, TimeUnit.MILLISECONDS)
                    .apply {
                        list.forEach {
                            addInterceptor(it)
                        }
                    }
                    .build()
 
    private fun newRetrofit(mOkHttpClient: OkHttpClient): Retrofit =
            Retrofit.Builder()
                    .apply {
                        baseUrl(application.ROOT_URL_RELEASE)
                    }
                    .addConverterFactory(
                            GsonConverterFactory.create(
                                    GsonBuilder()
                                            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                                            .create()
                            )
                    )
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(mOkHttpClient)
                    .build()
 
    companion object {
 
        @Volatile
        lateinit var instance: RetrofitFactory
 
        @JvmStatic
        @Synchronized
        fun init(application: CommonApplication) {
            instance = RetrofitFactory(application)
        }
 
        /**
         * 执行请求返回结果
         */
        fun <T> executeResult(observable: Observable<Response<T>>, subscriber: ResultObserver<T>) {
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(subscriber)
        }
 
        fun withProgressListeningRetrofit(listener: NetWorkProgressListener?): Retrofit {
            return if (listener == null) {
                instance.retrofit
            } else {
                val c = instance.newOkHttpClient(listOf(NetWorkProgressInterceptor(listener)))
                instance.newRetrofit(c)
            }
        }
    }
}