//Retortfit工具类
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
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 RetrofitManager<T> {
private final String BASE_URL = "http://172.17.8.100/small/";
private static RetrofitManager mRetrofitManager;
public static synchronized RetrofitManager getInstance() {
if (mRetrofitManager == null) {
mRetrofitManager = new RetrofitManager();
}
return mRetrofitManager;
}
private BaseApis mBaseApis;
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页面
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PartMap;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
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);
}
//IModelImpl层和P层
public class IModelImpl<T> implements IModel {
@Override
public void requestData(String url, Map<String, String> params, final Class clazz, final MyCallBack callBack) {
Map<String, RequestBody> map = RetrofitManager.getInstance().generateRequestBody(params);
RetrofitManager.getInstance().postFormBody(url, map).result(new RetrofitManager.HttpListener() {
@Override
public void onSuccess(String data) {
try{
Object o = new Gson().fromJson(data, clazz);
if(callBack != null){
callBack.onSuccess(o);
}
}catch (Exception e){
e.printStackTrace();
if(callBack != null){
callBack.onFail(e.getMessage());
}
}
}
@Override
public void onFail(String error) {
if(callBack != null){
callBack.onFail(error);
}
}
});
}
}
public class IPresenterImpl implements IPresenter {
private IModelImpl model;
private IView iView;
public IPresenterImpl(IView iView) {
this.iView = iView;
model = new IModelImpl();
}
@Override
public void startRequest(String url, Map<String, String> params, Class clazz) {
model.requestData(url, params, clazz, new MyCallBack() {
@Override
public void onSuccess(Object data) {
iView.getDataSuccess(data);
}
@Override
public void onFail(String error) {
iView.getDataFail(error);
}
});
}
public void onDetach() {
if (model != null) {
model = null;
}
if (iView != null) {
iView = null;
}
}
}