《》
package com.bawei.myshouye.net;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
public class HttpUtils {
private String baseUrl = “http://172.17.8.100”;
//传递头参的构造方法
private Map<String, String> headMap = new HashMap<>();
private HttpListener httpListener;
public HttpUtils setHead(Map<String, String> headMap) {
this.headMap = headMap;
return this;
}
//get请求
public HttpUtils get(String url, Map<String, String> map) {
//当写了请求头map不调用时 在这里判断 new一下否则 会展示不出数据
if (map == null) {
map = new HashMap<>();
}
//缓存sd卡的绝对路径
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
long size = 1024 * 10;
//ok拦截器
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// request.newBuilder().addHeader() 可以在此添加请求头
return chain.proceed(request);
}
}).cache(new Cache(file, size))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
HttpService service = retrofit.create(HttpService.class);
service.get(url, headMap, map).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
//获取服务端返回的数据
try {
String data = responseBody.string();
httpListener.success(data);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
//错误的信息
String error = e.getMessage();
httpListener.fail(error);
}
@Override
public void onComplete() {
}
});
return this;
}
//通过接口回调将以上数据返回到调用 的类
public void result(HttpListener httpListener) {
this.httpListener = httpListener;
}
public interface HttpListener {
void success(String data);
void fail(String error);
}
}
《model》
public interface Model {
interface CallBackListener {
void success(int type, String data);
void fail(int type, String error);
}
void doget(int type, String url, Map<String, String> map, CallBackListener listener);
1
2
3
4
}
《modeliml》
public class ModelIml implements Model {
@Override
public void doget(final int type, String url, Map<String, String> map, final CallBackListener listener) {
new HttpUtils().get(url, map).result(new HttpUtils.HttpListener() {
@Override
public void success(String data) {
listener.success(type, data);
}
@Override
public void fail(String error) {
listener.fail(type, error);
}
});
}
1
2
3
4
5
6
7
8
}
作者:偏执青年
来源:优快云
原文:https://blog.youkuaiyun.com/weixin_44666694/article/details/89597315
版权声明:本文为博主原创文章,转载请附上博文链接!
==========
《MainView 》
public interface MainView {
void success(int type, String data);//type 做为区分 是哪个请求
void fail(int type, String error);
}
《MainModel 》
public interface MainModel {
interface OnCallBackListener {
void success(int type, String data);//type 做为区分 是哪个请求
void fail(int type, String error);
}
void doShopCar(int type, String url, OnCallBackListener listener);
}
《MainModelIml 》
public class MainModelIml implements MainModel {
private OnCallBackListener listener;
private int type;
@Override
public void doShopCar(int type, String url, OnCallBackListener listener) {
this.listener = listener;
this.type = type;
//请求购物车数据
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
final Message message = Message.obtain();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
message.what = 1001;
message.obj = e.getMessage();
handler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
message.what = 1000;
message.obj = response.body().string();
handler.sendMessage(message);
}
});
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1000) {//成功
String data = (String) msg.obj;
listener.success(type, data);
} else {
//失败
String error = (String) msg.obj;
listener.success(type, error);
}
}
};
}
《MainPresenter 》
public interface MainPresenter {
void doShopCar(int type, String url);
}
《MainPresenterIml 》
public class MainPresenterIml implements MainPresenter, MainModel.OnCallBackListener {
private MainModel mainModel;
private MainView mainView;
public MainPresenterIml(MainModel mainModel, MainView mainView) {
this.mainModel = mainModel;
this.mainView = mainView;
}
@Override
public void doShopCar(int type, String url) {
mainModel.doShopCar(type, url, this);
}
@Override
public void success(int type, String data) {
mainView.success(type, data);
}
@Override
public void fail(int type, String error) {
mainView.fail(type, error);
}
//销毁
public void destory() {
if (mainView != null) {
mainView = null;
}
if (mainModel != null) {
mainModel = null;
}
System.gc();
}
}