MVP+Retrofit封装网络请求

//泛型类

public class MessageBean<T> {
    private String result;
    private T data;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}


//http
public interface ApiService {
    @GET("93app/data.do")
    Flowable<MessageBean<List<News>>> getNews(@QueryMap Map<String, String> map);
}



public class RetrofitUtils {
    private static volatile RetrofitUtils instance;

    private ApiService apiService;

    private RetrofitUtils() {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl("http://www.93.gov.cn/")
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    public static RetrofitUtils getInstance() {
        if (instance == null) {
            synchronized (RetrofitUtils.class) {
                if (null == instance) {
                    instance = new RetrofitUtils();
                }
            }
        }
        return instance;
    }

    public ApiService getApiService() {
        return apiService;
    }
}

//model
public interface IModel {
    void getData(Map<String, String> map);
}

public class NewsModel implements IModel {
    private NewsPresenter presenter;
    public NewsModel(NewsPresenter presenter){
        this.presenter = presenter;
    }
    @Override
    public void getData(Map<String, String> map) {
        Flowable<MessageBean<List<News>>> flowable = RetrofitUtils.getInstance().getApiService()
                .getNews(map);
        presenter.getNews(flowable);
    }
}

//presenter
public interface BasePresenter {
    void getData(Map<String, String> map);
}

public class NewsPresenter implements BasePresenter {
    private IView iv;
    private DisposableSubscriber subscriber;

    public void attachView(IView iv) {
        this.iv = iv;
    }

    public void detachView() {
        if (iv != null) {
            iv = null;
        }
        if (subscriber != null) {
            if (!subscriber.isDisposed()) {
                subscriber.dispose();
            }
        }
    }

    @Override
    public void getData(Map<String, String> map) {
        IModel model = new NewsModel(this);
        model.getData(map);
    }

    public void getNews(Flowable<MessageBean<List<News>>> flowable) {
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<MessageBean<List<News>>>() {
                    @Override
                    public void onNext(MessageBean<List<News>> listMessageBean) {
                        if (listMessageBean != null) {
                            List<News> list = listMessageBean.getData();
                            if (list != null) {
                                iv.onSuccess(list);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}

//view

public interface IView {
    void onSuccess(Object o);
    void onFailed(Exception e);
}

public class MainActivity extends AppCompatActivity  implements IView {
    private static final String TAG = "MainActivity";

    private NewsPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Map<String, String> map = new HashMap<>();
        map.put("channelId", "0");
        map.put("startNum", "0");

        presenter = new NewsPresenter();
        presenter.attachView(this);
        presenter.getData(map);
    }

    @Override
    public void onSuccess(Object o) {
        if (o instanceof List) {
            List<News> news = (List<News>) o;
            Log.i(TAG, "onSuccess: " + news.size());
        }
    }

    @Override
    public void onFailed(Exception e) {
        Log.e(TAG, "onFailed: " + e.getMessage());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
}




 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值