RetrofitUtil
public class RetrofitUtil {
private static volatile RetrofitUtil instance;
private final Retrofit retrofit;
private RetrofitUtil(String baseUrl){
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static RetrofitUtil getInstance(String baseUrl){
if (instance == null) {
synchronized (RetrofitUtil.class){
if (instance == null) {
instance = new RetrofitUtil(baseUrl);
}
}
}
return instance;
}
public Retrofit getRetrofit(){
return retrofit;
}
}
ApiService
public interface ApiService {
@GET("ad/getAd")
Flowable<Product> getProduct(@QueryMap Map<String,String> map);
}
IModel
public interface IModel {
void getProduct(String baseUrl, Map<String,String> map, LoadNetListener loadNetListener);
}
ModelImpl
public class ModelImpl implements IModel {
private static final String TAG = "ModelImpl";
private DisposableSubscriber<Product> subscriber;
@Override
public void getProduct(String baseUrl, Map<String, String> map, final LoadNetListener loadNetListener) {
Log.e(TAG, "getProduct: ---"+map.toString());
Flowable<Product> flowable = RetrofitUtil.getInstance(baseUrl).getRetrofit().create(ApiService.class).getProduct(map);
subscriber = (DisposableSubscriber)flowable.subscribeOn(Schedulers.io())//在io线程进行网络请求
.observeOn(AndroidSchedulers.mainThread())//回到主线程请求结果
.subscribeWith(new DisposableSubscriber(){
@Override
public void onNext(Object o) {
Log.e(TAG, "onNext: ----next");
loadNetListener.loadSuccess(o);
}
@Override
public void onError(Throwable t) {
Log.e(TAG, "onError: ----"+t.toString());
loadNetListener.loadFailed(t);
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: ----complete");
}
});
}
}
IView
public interface IView {
void getData(Object o);
void getFailed(Exception e);
}
IPresenter
public interface IPresenter {
void getDataToView(String baseUrl, Map<String,String> map);
}
PresenterImpl
public class PresenterImpl implements IPresenter {
private IView iView;
private static final String TAG = "PresenterImpl";
public PresenterImpl(IView iView){
this.iView = iView;
}
@Override
public void getDataToView(String baseUrl, Map<String, String> map) {
ModelImpl model = new ModelImpl();
model.getProduct(baseUrl, map, new LoadNetListener() {
@Override
public void loadSuccess(Object netData) {
iView.getData(netData);
}
@Override
public void loadFailed(Object netData) {
Log.e(TAG, "loadFailed: ----展示商品失败");
}
});
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements IView {
private PresenterImpl presenter;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HashMap<String, String> map = new HashMap<>();
map.put("","");
presenter = new PresenterImpl(this);
presenter.getDataToView("https://www.zhaoapi.cn/",map);
}
@Override
public void getData(Object o) {
Product product = (Product) o;
Log.e(TAG, "getData: ----"+product.getData().get(0).getTitle());
}
@Override
public void getFailed(Exception e) {
Log.e(TAG, "getData: ----展示失败");
}
}