《view层》
public interface Views {
void success(int type, String data);
void fail(int type, 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 map, CallBackListener listener);
}
public class ModelIml implements Model {
@Override
public void doget(final int type, String url, Map
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);
}
});
}
}
《presenter层》
public interface Presenter {
void doget(int type, String url, Map
}
public class PresenterIml implements Presenter, Model.CallBackListener {
private Model model;
private Views views;
public PresenterIml(Model model, Views views) {
this.model = model;
this.views = views;
}
@Override
public void doget(int type, String url, Map map) {
model.doget(type, url, map, this);
}
@Override
public void success(int type, String data) {
views.success(type, data);
}
@Override
public void fail(int type, String error) {
views.fail(type, error);
}
public void destory() {
if (model != null) {
model = null;
}
if (views != null) {
views = views;
}
}
}
《HttpUtils网络请求层》
public class HttpUtils {
private String baseUrl = “http://172.17.8.100”;
//传递头参的构造方法
private Map
private HttpListener httpListener;
public HttpUtils setHead(Map headMap) {
this.headMap = headMap;
return this;
}
//get请求
public HttpUtils get(String url, Map 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() {
@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);
}
}
== 《HttpService》==
public interface HttpService {
@GET
Observable get(@Url String url, @QueryMap Map
, @HeaderMap Map
}