导入依赖
app下的gradle
// 网络请求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"
//okhttp依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
根目录下的gradle
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'
}
创建类如下图 分别为MVP层
BaseApis
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
public interface BaseApis {
@GET
Observable get(@Url String urlData);
@POST
Observable post(@Url String urlData, @QueryMap Map map);
@DELETE
Observable delete(@Url String urlData,@QueryMap Map map);
@PUT
Observable put(@Url String urlData,@QueryMap Map map);
}
Apis
放入接口后缀,例如:
public static final String Path="接口";
Retrofit
import java.util.Map;
import java.util.concurrent.TimeUnit;
import asus.com.qing.jjyk1.BaseApis;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class RetrofitUtils {
public static RetrofitUtils retrofitUtils;
public static String BASE_URL="统一的接口前缀";
private final BaseApis baseApis;
//单例
public static RetrofitUtils getRetrofitUtils() {
if (retrofitUtils==null){
synchronized (RetrofitUtils.class){
if (null==retrofitUtils){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
public RetrofitUtils(){
HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient mClient=new OkHttpClient.Builder()
.connectTimeout(10,TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(10,TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(mClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
baseApis = retrofit.create(BaseApis.class);
}
//get
public void get(String urlData,HttpListener listener){
baseApis.get(urlData)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObsetver(listener));
}
//post
public void post(String urlData, Map map, HttpListener listener){
baseApis.post(urlData,map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObsetver(listener));
}
//put
public void put(String urlData, Map map, HttpListener listener){
baseApis.post(urlData,map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObsetver(listener));
}
//delete
public void delete(String urlData, Map map, HttpListener listener){
baseApis.post(urlData,map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObsetver(listener));
}
//观察者
public Observer getObsetver(final HttpListener listener){
Observer observer= new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
if (listener!=null){
listener.onFail(e.getMessage());
}
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String string = responseBody.string();
if (listener!=null){
listener.onSuccess(string);
}
}catch (Exception e){
e.printStackTrace();
if (listener!=null){
listener.onFail(e.getMessage());
}
}
}
};
return observer;
};
public HttpListener listener;
public void setListener(HttpListener httplistener) {
this. listener = httplistener;
}
public interface HttpListener{
void onSuccess(String data);
void onFail(String error);
}
}
MyCallBack
public interface MyCallBack {
void onSuccess(Object data);
void onFail(Exception e);
}
IView
public interface IView {
void onSuccessData(Object data);
void onFailData(Exception e);
}
IPresenter
import java.util.Map;
public interface Ipresenter {
void onRequestGet(String urlData,Class clazz);
void onRequestPost(String urlData, Map map,Class clazz);
void onRequestPut(String urlData, Map map,Class clazz);
void onRequestDelete(String urlData, Map map,Class clazz);
}
PresenterImpl
import java.util.Map;
import asus.com.qing.jjyk1.callback.MyCallBack;
import asus.com.qing.jjyk1.model.ModelImpl;
import asus.com.qing.jjyk1.view.IView;
public class PresenterImpl implements Ipresenter {
private ModelImpl model;
private IView iView;
public PresenterImpl(IView iView) {
this.model = new ModelImpl();
this.iView = iView;
}
//get
@Override
public void onRequestGet(String urlData, Class clazz) {
model.onRequestGetData(urlData, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
iView.onSuccessData(data);
}
@Override
public void onFail(Exception e) {
iView.onFailData(e);
}
});
}
//post
@Override
public void onRequestPost(String urlData, Map map, Class clazz) {
model.onRequestPostData(urlData, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
iView.onSuccessData(data);
}
@Override
public void onFail(Exception e) {
iView.onFailData(e);
}
});
}
//put
@Override
public void onRequestPut(String urlData, Map map, Class clazz) {
model.onRequestPutData(urlData, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
iView.onSuccessData(data);
}
@Override
public void onFail(Exception e) {
iView.onFailData(e);
}
});
}
//delete
@Override
public void onRequestDelete(String urlData, Map map, Class clazz) {
model.onRequestDeleteData(urlData, map, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
iView.onSuccessData(data);
}
@Override
public void onFail(Exception e) {
iView.onFailData(e);
}
});
}
//解绑
public void detach(){
iView=null;
model=null;
}
}
IModel
import java.util.Map;
import asus.com.qing.jjyk1.callback.MyCallBack;
public interface IModel {
void onRequestGetData(String urlData, Class clazz, MyCallBack myCallBack);
void onRequestPostData(String urlData, Map map, Class clazz,MyCallBack myCallBack);
void onRequestPutData(String urlData, Map map,Class clazz,MyCallBack myCallBack);
void onRequestDeleteData(String urlData, Map map,Class clazz,MyCallBack myCallBack);
}
ModelImpl
import com.google.gson.Gson;
import java.util.Map;
import asus.com.qing.jjyk1.callback.MyCallBack;
import asus.com.qing.jjyk1.utils.RetrofitUtils;
public class ModelImpl implements IModel {
//get
@Override
public void onRequestGetData(String urlData, final Class clazz, final MyCallBack myCallBack) {
RetrofitUtils.getRetrofitUtils().get(urlData, new RetrofitUtils.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
e.printStackTrace();
if (myCallBack != null) {
myCallBack.onFail(e);
}
}
}
@Override
public void onFail(String error) {
}
});
}
//post
@Override
public void onRequestPostData(String urlData, Map map, final Class clazz, final MyCallBack myCallBack) {
RetrofitUtils.getRetrofitUtils().post(urlData, map, new RetrofitUtils.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
e.printStackTrace();
if (myCallBack != null) {
myCallBack.onFail(e);
}
}
}
@Override
public void onFail(String error) {
}
});
}
//put
@Override
public void onRequestPutData(String urlData, Map map, final Class clazz, final MyCallBack myCallBack) {
RetrofitUtils.getRetrofitUtils().put(urlData, map, new RetrofitUtils.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
e.printStackTrace();
if (myCallBack != null) {
myCallBack.onFail(e);
}
}
}
@Override
public void onFail(String error) {
}
});
}
//delete
@Override
public void onRequestDeleteData(String urlData, Map map, final Class clazz, final MyCallBack myCallBack) {
RetrofitUtils.getRetrofitUtils().delete(urlData, map, new RetrofitUtils.HttpListener() {
@Override
public void onSuccess(String data) {
try {
Object o = new Gson().fromJson(data, clazz);
if (myCallBack != null) {
myCallBack.onSuccess(o);
}
} catch (Exception e) {
e.printStackTrace();
if (myCallBack != null) {
myCallBack.onFail(e);
}
}
}
@Override
public void onFail(String error) {
}
});
}
}