| | |
| | | import com.xw.common.net.interceptor.HeaderInterceptor |
| | | import com.xw.common.net.interceptor.LoggingInterceptor |
| | | import okhttp3.HttpUrl.Companion.toHttpUrl |
| | | import okhttp3.Interceptor |
| | | import okhttp3.MediaType.Companion.toMediaTypeOrNull |
| | | import okhttp3.OkHttpClient |
| | | import okhttp3.RequestBody |
| | | import org.json.JSONArray |
| | | import org.json.JSONObject |
| | | import retrofit2.Retrofit |
| | | import retrofit2.converter.gson.GsonConverterFactory |
| | | import retrofit2.converter.scalars.ScalarsConverterFactory |
| | |
| | | |
| | | } |
| | | |
| | | fun <T> create(baseUrl: String, c: Class<T>): T { |
| | | fun <T> create( |
| | | baseUrl: String, |
| | | c: Class<T>, |
| | | interceptor: Interceptor = HeaderInterceptor() |
| | | ): T { |
| | | return Retrofit.Builder() |
| | | .baseUrl(baseUrl.toHttpUrl()) |
| | | .client(provideOKHttpClient()) |
| | | .client(provideOKHttpClient(interceptor)) |
| | | .addConverterFactory(ScalarsConverterFactory.create()) |
| | | .addConverterFactory(GsonConverterFactory.create()) |
| | | .build() |
| | | .create(c) |
| | | } |
| | | |
| | | private fun provideOKHttpClient(): OkHttpClient { |
| | | private fun provideOKHttpClient(interceptor: Interceptor): OkHttpClient { |
| | | val builder = OkHttpClient.Builder() |
| | | .connectTimeout(30, TimeUnit.SECONDS) |
| | | .readTimeout(30, TimeUnit.SECONDS) |
| | | .writeTimeout(30, TimeUnit.SECONDS) |
| | | .retryOnConnectionFailure(true) |
| | | .addInterceptor(HeaderInterceptor()) |
| | | .addInterceptor(interceptor) |
| | | .addInterceptor(LoggingInterceptor()) |
| | | return builder.build() |
| | | } |