Retrofit请求工具类

package bwei.com.myyikezhong.utils;

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

/**
 * author:Created by WangZhiQiang on 2018/3/8 0008.
 */

public class Util {

    private volatile static Util util;
        private Util() {

        }
        public static Util getmInstance() {
            if (util == null) {
                synchronized (Util.class) {
                    if (util == null) {
                        util = new Util();
                    }
                }
            }
            return util;
        }

        public TestService getNetData(String s) {
            //ok拦截日志
            OkHttpClient httpClient = new OkHttpClient.Builder().
                    addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                    .build();
            Retrofit retrofit = new Retrofit.Builder().baseUrl(s)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient)
                    .build();
            TestService testService = retrofit.create(TestService.class);
            return testService;
        }
}

你可能感兴趣的:(Retrofit请求工具类)