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;
    }

}

你可能感兴趣的:(android Retrofit简单封装)