导依赖
// 网络请求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"
//在工程的build.gradle中
ext {
// Sdk and tools
minSdkVersion = 19
targetSdkVersion = 28
compileSdkVersion = 28
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'
}
RetrofitUtils工具类
public class RetrofitUtils {
private static RetrofitUtils instance;
private OkHttpClient client;
private final String BASE_URL="http://172.17.8.100/small/";
private BaseApis baseApis;
//创建单例
public static RetrofitUtils getInstance(){
if(instance==null){
synchronized (RetrofitUtils.class){
instance=new RetrofitUtils();
}
}
return instance;
}
//构造方法
private RetrofitUtils(){
//日志拦截
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
HttpLoggingInterceptor interceptor1 = interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client=new OkHttpClient.Builder()
.addInterceptor(interceptor1)
.writeTimeout(2000, TimeUnit.SECONDS)
.readTimeout(2000,TimeUnit.SECONDS)
.connectTimeout(2000,TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.client(client)
.build();
baseApis=retrofit.create(BaseApis.class);
}
//get请求
public RetrofitUtils getRequest(String url,HttpCallBack httpCallBack){
baseApis.getRequest(url)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(httpCallBack));
return instance;
}
//post请求
public RetrofitUtils postRequest(String url, Map<String,String> map,HttpCallBack httpCallBack){
if(map==null){
map=new HashMap<>();
}
baseApis.postRequest(url,map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver(httpCallBack));
return instance;
}
private Observer getObserver(final HttpCallBack callBack) {
Observer observer=new Observer<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
if(callBack!=null){
callBack.onFail(e.getMessage());
}
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String result = responseBody.string();
if(callBack!=null){
callBack.onSuccess(result);
}
}catch (Exception e){
e.printStackTrace();
if(callBack!=null){
callBack.onFail(e.getMessage());
}
}
}
};
return observer;
}
//接口传值
public HttpCallBack httpCallBack;
public void setHttpCallBack(HttpCallBack callBack){
this.httpCallBack=callBack;
}
public interface HttpCallBack{
void onSuccess(String data);//成功
void onFail(String error);//失败
}
}
BaseApis类
public interface BaseApis {
//get请求
@GET
Observable<ResponseBody> getRequest(@Url String url);
//post请求
@POST
Observable<ResponseBody> postRequest(@Url String url, @QueryMap Map<String,String> map);
}
Model层
public interface IModel {
void requestData(String path, Map<String,String> map, Class clazz, MCallBack mCallBack);
void requestDataGet(String path, Class clazz, MCallBack mCallBack);
}
Model层实现类
public class IModelmpl implements IModel {
@Override
public void requestData(String path, Map<String, String> map, final Class clazz, final MCallBack mCallBack) {
RetrofitUtils.getInstance().postRequest(path, map, new RetrofitUtils.HttpCallBack() {
@Override
public void onSuccess(String data) {
try {
Gson gson=new Gson();
Object o = gson.fromJson(data, clazz);
mCallBack.getSuccessData(o);
}catch (Exception e){
e.printStackTrace();
mCallBack.getFailData(e.getMessage());
}
}
@Override
public void onFail(String error) {
mCallBack.getFailData(error);
}
});
}
@Override
public void requestDataGet(String path, final Class clazz, final MCallBack mCallBack) {
RetrofitUtils.getInstance().getRequest(path, new RetrofitUtils.HttpCallBack() {
@Override
public void onSuccess(String data) {
try {
Gson gson=new Gson();
Object o = gson.fromJson(data,clazz);
mCallBack.getSuccessData(o);
}catch (Exception e){
e.printStackTrace();
mCallBack.getFailData(e.getMessage());
}
}
@Override
public void onFail(String error) {
mCallBack.getFailData(error);
}
});
}
}
Presenter层
public interface IPresenter {
void startRequest(String path, Map<String,String> map,Class clazz);
void startRequestGet(String path,Class clazz);
}
Presenter层实现类
public class IPresenterImpl implements IPresenter {
private IView iView;
private IModelmpl iModelmpl;
public IPresenterImpl(IView iView) {
this.iView = iView;
iModelmpl=new IModelmpl();
}
@Override
public void startRequest(String path, Map<String, String> map, Class clazz) {
iModelmpl.requestData(path, map, clazz, new MCallBack() {
@Override
public void getSuccessData(Object data) {
iView.requestSuccess(data);
}
@Override
public void getFailData(String error) {
iView.requestFail(error);
}
});
}
@Override
public void startRequestGet(String path, Class clazz) {
iModelmpl.requestDataGet(path, clazz, new MCallBack() {
@Override
public void getSuccessData(Object data) {
iView.requestSuccess(data);
}
@Override
public void getFailData(String error) {
iView.requestFail(error);
}
});
}
//解除绑定
public void detachView(){
if(iModelmpl!=null){
iModelmpl=null;
}
if(iView!=null){
iView=null;
}
}
}
View 层
public interface IView<T> {
//请求成功的方法
void requestSuccess(T data);
//请求失败的方法
void requestFail(String error);
}