1. 程式人生 > >android Retrofit簡單封裝

android Retrofit簡單封裝

package com.dejun.commonsdk.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Author:DoctorWei
 * Time:2018/12/4 16:37
 * Description:
 * email:
[email protected]
*/ public class RetrofitClient { private static RetrofitClient retrofitClient; private RetrofitClient() { } /** * 懶漢多執行緒單例 * * @return */ public static RetrofitClient getInstance() { if (retrofitClient == null) { synchronized (RetrofitClient.class) { if (retrofitClient == null) { retrofitClient = new RetrofitClient(); } } } return retrofitClient; } public Retrofit getRetrofit(String baseUrl){ Retrofit retrofit=new Retrofit.Builder() .client(setOkHttpClick()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(baseUrl) .build(); return retrofit; } private OkHttpClient setOkHttpClick() { OkHttpClient okHttpClient=new OkHttpClient().newBuilder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15,TimeUnit.SECONDS) .addInterceptor(new HttpLoggingInterceptor()) .retryOnConnectionFailure(true) .build(); return okHttpClient; } }