封装Retrofit工具类

package com.example.retrofit;

import java.util.concurrent.TimeUnit;

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

/**
 * Created by DELL on 2018/7/9.
 */

public class RetrofitManager {

    private static String BASE_URL = "https://www.zhaoapi.cn/";
    private final Retrofit retrofit;

    private static class SingleHolder {

        private static final RetrofitManager _INSTANCE = new RetrofitManager(BASE_URL);

    }

    public static RetrofitManager getDefault() {

        return SingleHolder._INSTANCE;

    }

    private RetrofitManager(String baseUrl) {

        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(buildokHttpclient())
                .build();

    }

    private OkHttpClient buildokHttpclient() {

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .build();

        return client;
    }

    public  T create(Class Clazz) {

        return retrofit.create(Clazz);

    }

}

你可能感兴趣的:(封装Retrofit工具类)