导入依赖
// 网络请求Retrofit & OkHttp & Rxjava
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.interceptor"
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.converterGson"
implementation "io.reactivex:rxandroid:$rootProject.rxandroid"
implementation "io.reactivex:rxjava:$rootProject.rxjava"
implementation "com.hwangjr.rxbus:rxbus:$rootProject.rxbus"
implementation "com.squareup.retrofit2:adapter-rxjava:$rootProject.adapterRxjava"
大项目里在最后添加(可以根据自己的版本 和sdk修改)
ext {
// Sdk and tools
minSdkVersion = 19
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
//support版本
supportVersion = '27.1.0'
constraintLayout = '1.0.2'
junit = '4.12'
testRunner = '1.0.1'
espressoCore = '3.0.1'
interceptor = "3.11.0"
retrofit = '2.3.0'
converterGson = '2.3.0'
rxandroid = '1.2.1'
rxjava = '1.3.0'
rxbus = '1.0.6'
adapterRxjava = '2.0.2'
}
请求代码
public class RetrofitManager<T> {
private final String BASE_URL = "http://172.17.8.100/small/";
private static RetrofitManager mRetrofitManager;
private BaseApis mBaseApis;
public static synchronized RetrofitManager getInstance() {
if (mRetrofitManager == null) {
mRetrofitManager = new RetrofitManager();
}
return mRetrofitManager;
}
public RetrofitManager() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(15, TimeUnit.SECONDS);
builder.writeTimeout(15, TimeUnit.SECONDS);
builder.addInterceptor(interceptor);
builder.retryOnConnectionFailure(true);
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.client(client)
.build();
mBaseApis = retrofit.create(BaseApis.class);
}
/**
* 可以这样生成Map<String, RequestBody> requestBodyMap
* Map<String, String> requestDataMap这里面放置上传数据的键值对。
*/
public Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
Map<String, RequestBody> requestBodyMap = new HashMap<>();
for (String key : requestDataMap.keySet()) {
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
requestBodyMap.put(key, requestBody);
}
return requestBodyMap;
}
/**
* get请求
*/
public RetrofitManager get(String url) {
mBaseApis.get(url)
//后台执行在哪个线程中
.subscribeOn(Schedulers.io())
//最终完成后执行在哪个线程
.observeOn(AndroidSchedulers.mainThread())
//设置我们的rxJava
.subscribe(observer);
return mRetrofitManager;
}
/**
* 表单post请求
*/
public RetrofitManager postFormBody(String url, Map<String, RequestBody> map) {
if (map == null) {
map = new HashMap<>();
}
mBaseApis.postFormBody(url, map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
return mRetrofitManager;
}
/**
* 普通post请求
*/
public RetrofitManager post(String url, Map<String, String> map) {
if (map == null) {
map = new HashMap<>();
}
mBaseApis.post(url, map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
return mRetrofitManager;
}
private Observer observer = new Observer<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
try {
String data = responseBody.string();
if (listener != null) {
listener.onSuccess(data);
}
} catch (IOException e) {
e.printStackTrace();
if (listener != null) {
listener.onFail(e.getMessage());
}
}
}
@Override
public void onError(Throwable e) {
if (listener != null) {
listener.onFail(e.getMessage());
}
}
@Override
public void onCompleted() {
}
};
private HttpListener listener;
public void result(HttpListener listener) {
this.listener = listener;
}
public interface HttpListener {
void onSuccess(String data);
void onFail(String error);
}
}
BaseApis接口
public interface BaseApis <T> {
@GET
Observable<ResponseBody> get(@Url String url);
@POST
Observable<ResponseBody> post(@Url String url, @QueryMap Map<String, String> map);
@Multipart
@POST
Observable<ResponseBody> postFormBody(@Url String url, @PartMap Map<String, RequestBody> requestBodyMap);
}