package com.example.rj; import android.util.Log; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by FLOWER on 2017/11/17. */ public class RetrofirUitls { private static RetrofirUitls retrofirUitls; public RetrofirUitls() { } public static RetrofirUitls getIntence(){ if (retrofirUitls == null){ synchronized (RetrofirUitls.class){ if (retrofirUitls == null){ retrofirUitls = new RetrofirUitls(); } } } return retrofirUitls; } private static Retrofit retrofit; public static synchronized Retrofit getRetrofit(String url){ HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { Log.i("xxx",message); } }); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .connectTimeout(5000, TimeUnit.SECONDS) .readTimeout(5000,TimeUnit.SECONDS) .retryOnConnectionFailure(false) .build(); if (retrofit == null){ retrofit = new Retrofit.Builder().baseUrl(url) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); } return retrofit; } public <T>T getApiService(String url,Class<T> cl){ Retrofit retrofit = getRetrofit(url);//得到retrofit return retrofit.create(cl);//返回的就是网络接口对象 } }
Retrofit+RxJava网络请求框架
最新推荐文章于 2020-11-08 15:21:11 发布