依赖
//引入okhttp
compile 'com.squareup.okhttp3:okhttp:3.5.0'
//引入retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
//引入rxjava
compile 'io.reactivex.rxjava2:rxjava:2.0.4'
//引入Log拦截器,方便DEBUG模式输出log信息
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
//引入rxjava适配器,方便rxjava与retrofit的结合
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
//引入json转换器,方便将返回的数据转换为json格式
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//引入rxandroid
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
核心类,在使用的时候可以根据自己的需求改变拦截器
package com.lvr.retrofitclient.client;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* RetrofitClient
* Created by Tamic on 2016-06-15.
* {@link # https://github.com/NeglectedByBoss/RetrofitClient}
*/
public class RetrofitClient {
private static final int DEFAULT_TIMEOUT = 20;
private BaseApiService apiService;
private static OkHttpClient okHttpClient;
public static String baseUrl = BaseApiService.Base_URL;
private static Context mContext;
private static RetrofitClient sNewInstance;
private static ErrorTransformer transformer = new ErrorTransformer();
private static Retrofit retrofit;
private Cache cache = null;
private File httpCacheDirectory;
private static Retrofit.Builder builder =
new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(baseUrl);
private static OkHttpClient.Builder httpClient =
new OkHttpClient.Builder()
.addNetworkInterceptor(
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
private static Class extends Class> aClass;
//默认baseUrl的单例
private static class SingletonHolder {
private static RetrofitClient INSTANCE = new RetrofitClient(
mContext);
}
public static RetrofitClient getInstance(Context context) {
if (context != null) {
mContext = context;
}
return SingletonHolder.INSTANCE;
}
//自己提供url
public static RetrofitClient getInstance(Context context, String url) {
if (context != null) {
mContext = context;
}
return new RetrofitClient(context, url);
}
//不仅有url还有headers
public static RetrofitClient getInstance(Context context, String url, Map headers) {
if (context != null) {
mContext = context;
}
return new RetrofitClient(context, url, headers);
}
private RetrofitClient() {
}
private RetrofitClient(Context context) {
this(context, baseUrl, null);
}
private RetrofitClient(Context context, String url) {
this(context, url, null);
}
private RetrofitClient(Context context, String url, Map headers) {
//url为空,则默认使用baseUrl
if (TextUtils.isEmpty(url)) {
url = baseUrl;
}
//缓存地址
if (httpCacheDirectory == null) {
httpCacheDirectory = new File(mContext.getCacheDir(), "app_cache");
}
try {
if (cache == null) {
cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
}
} catch (Exception e) {
Log.e("OKHttp", "Could not create http cache", e);
}
//okhttp创建了
okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.cache(cache)
.addInterceptor(new BaseInterceptor(headers))
.addInterceptor(new CaheInterceptor(context))
.addNetworkInterceptor(new CaheInterceptor(context))
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.build();
//retrofit创建了
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(url)
.build();
}
public void get(String url, Map parameters, Observer> observer) {
apiService.executeGet(url, parameters)
.compose(schedulersTransformer)
.compose(transformer)
.subscribe(observer);
}
public void post(String url, Map parameters, Observer> observer) {
apiService.executePost(url, parameters)
.compose(schedulersTransformer)
.compose(transformer)
.subscribe(observer);
}
public void json(String url, RequestBody jsonStr, Observer
apiService.json(url, jsonStr)
.compose(schedulersTransformer)
.compose(transformer)
.subscribe(observer);
}
public void upload(String url, RequestBody requestBody, Observer
apiService.upLoadFile(url, requestBody)
.compose(schedulersTransformer)
.compose(transformer)
.subscribe(observer);
}
//处理线程调度的变换
ObservableTransformer schedulersTransformer = new ObservableTransformer() {
@Override
public ObservableSource apply(Observable upstream) {
return ((Observable) upstream).subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
//处理错误的变换
private static class ErrorTransformer
@Override
public ObservableSource apply(Observable upstream) {
//onErrorResumeNext当发生错误的时候,由另外一个Observable来代替当前的Observable并继续发射数据
return (Observable
}
}
public static class HttpResponseFunc
@Override
public Observable
return Observable.error(ExceptionHandle.handleException(throwable));
}
}
public static class HandleFuc
@Override
public T apply(BaseResponse
//response中code码不会0 出现错误
if (!response.isOk())
throw new RuntimeException(response.getCode() + "" + response.getMsg() != null ? response.getMsg() : "");
return response.getData();
}
}
/**
* 创建默认url的api类
*
* @return ApiManager
*/
public RetrofitClient createBaseApi() {
apiService = create(BaseApiService.class);
return this;
}
/**
* create you ApiService
* Create an implementation of the API endpoints defined by the {@code service} interface.
*/
public
if (service == null) {
throw new RuntimeException("Api service is null!");
}
return retrofit.create(service);
}
}
需要说明的是其中的BaseApiService是我们的动态代理的接口类
而其中的IpResul是我们的解析类
package com.lvr.retrofitclient.client;
import com.lvr.retrofitclient.IpResult;
import java.util.Map;
import io.reactivex.Observable;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.PartMap;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
public interface BaseApiService
public static final String Base_URL = "http://tingapi.ting.baidu.com/";
@GET("v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0")
Observable getdata();
@GET("{url}")
Observable
@Path("url") String url,
@QueryMap Map maps
);
@POST("{url}")
Observable
@Path("url") String url,
@QueryMap Map maps);
@POST("{url}")
Observable
@Path("url") String url,
@Body RequestBody jsonStr);
@Multipart
@POST("{url}")
Observable
@Path("url") String url,
@Part("image\";
filename=\"image.jpg") RequestBody requestBody);
@POST("{url}")
Observable
@Path("url") String url,
@Part("userName") String description,
@PartMap() Map maps);
}
最简单的使用
RetrofitClient.getInstance(MainActivity.this);
【Android框架的使用|Retrofit+Rxjava封装类】