Retrofit2+Rxjava+MVP 实践

此博文根据前面两篇文章 Android MVP 架构初试 Android MVP 架构封装 再结合主流框架Retrofit2+Rxjava来个实践(实现聚合网周公解梦)

源码地址RxMVP

项目截图

Retrofit2+Rxjava+MVP 实践_第1张图片

Retrofit2+Rxjava 封装

JuHeService 数据请求接口

/**
 * 请求示例:
 * http://v.juhe.cn/dream/query
 * q:梦境关键字,如:黄金 需要utf8 urlencode
 * cid:指定分类,默认全部
 * full: 是否显示详细信息,1:是 0:否,默认0
 */
public interface JuHeService {
    @GET("dream/query")
    Observable>> getDreams(@QueryMap Map options);
}

HttpJuHeMethods 聚合解梦封装的方法

public class HttpJuHeMethods {

    public static final String BASE_URL = "http://v.juhe.cn/";

    private static final int DEFAULT_TIMEOUT = 5;

    private Retrofit retrofit;

    private JuHeService juheService;

    //构造方法私有
    private HttpJuHeMethods() {
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();

        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        juheService = retrofit.create(JuHeService.class);
    }

    //在访问HttpMethods时创建单例
    private static class SingletonHolder{
        private static final HttpJuHeMethods INSTANCE = new HttpJuHeMethods();
    }

    //获取单例
    public static HttpJuHeMethods getInstance(){
        return SingletonHolder.INSTANCE;
    }


    /**
     * 用来统一处理Http的resultCode,并将HttpResult的Data部分剥离出来返回给subscriber
     *
     * @param  Subscriber真正需要的数据类型,也就是Data部分的数据类型
     */
    private class HttpResultFunc implements Func1, T> {

        @Override
        public T call(HttpJuHeResult httpResult) {
            if (httpResult.getError_code() != 0) {
                throw new ApiException(httpResult.getError_code());
            }
            return httpResult.getResult();
        }
    }

    private  void toSubscribe(Observable observable, Subscriber subscriber){
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * 用于获取聚合笑话的数据
     * @param subscriber 由调用者传过来的观察者对象
     * @param options 访问参数
     */
    public void getJokesByHttpResultMap(Subscriber> subscriber, Map options){
//        juheService.getJokesByRxJavaHttpResult(options)
//                .map(new HttpResultFunc())
//                .subscribeOn(Schedulers.io())
//                .unsubscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(subscriber);
        Observable> observable = juheService.getDreams(options)
                .map(new HttpResultFunc>());
        toSubscribe(observable,subscriber);

    }
}

其中包含异常的处理

public class ApiException extends RuntimeException{
    public final static  int  TIME_MUST_10=209501;
    public final static  int  TIME_OTHER=209502;

    public ApiException(int resultCode) {
        this(getApiExceptionMessage(resultCode));
    }

    public ApiException(String detailMessage) {
        super(detailMessage);
    }

    /**
     * 由于服务器传递过来的错误信息直接给用户看的话,用户未必能够理解
     * 需要根据错误码对错误信息进行一个转换,在显示给用户
     * @param code
     * @return
     */
    private static String getApiExceptionMessage(int code){
        String message = "";
        switch (code) {
            case TIME_MUST_10:
                message = "必须为10位时间戳";
                break;
            case TIME_OTHER:
                message = "page、pagesize必须为int类型,time为10位时间戳";
                break;
            default:
                message = "未知错误";

        }
        return message;
    }
}

BaseMvp封装

请参考上篇文章 Android MVP 架构封装

Retrofit2+Rxjava+MVP实践

MvpView

public interface MvpView extends BaseView {
     //ListView的初始化
     void setListItem(List data);
     //Toast 消息
     void showMessage(String messgae);
}

MvpPresenter

public class MvpPresenter extends BasePresenter {
    private Context mContext;
    private Subscriber subscriber;
    private List mDatas;

    public MvpPresenter(Context context) {
        this.mContext = context;
    }

    //获取数据
    public void getData(String q) throws UnsupportedEncodingException {
        if (q.isEmpty()) {
            mView.showMessage("请输入解梦内容");
            return;
        }
        mView.showLoading();
        getDream(q);
    }

    public void onItemClick(int position) {

        List stringList = mDatas.get(position).getList();

        StringBuffer sbf = new StringBuffer();
        for (String s : stringList) {
            sbf.append(s).append("\n\n\n");
        }
        new SweetAlertDialog(mContext)
                .setTitleText(mDatas.get(position).getTitle())
                .setContentText(sbf.toString())
                .show();
    }

    private void getDream(String q) throws UnsupportedEncodingException {
        String content = URLDecoder.decode(q, "utf-8");
        Map options = new HashMap();
        options.put("key", "f86ed9f21931cd311deffada92b58ac7");
        options.put("full", "1");
        options.put("q", content);

        subscriber = new Subscriber>() {

            @Override
            public void onCompleted() {
                mView.hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.hideLoading();
                mView.showMessage(e.toString());
            }

            @Override
            public void onNext(List data) {
                for (JuHeDream  juheDream:data) {
                    Logger.e(juheDream.toString());
                }
                mDatas = data;
                mView.setListItem(mDatas);
            }
        };
        HttpJuHeMethods.getInstance().getJokesByHttpResultMap(subscriber,options);
    }

    public void destory(){
        subscriber.unsubscribe();
    }
}

布局文件




    

        

            

            

            
        

        

        

MainActivity

public class MainActivity extends BaseMvpActivity implements MvpView, AdapterView.OnItemClickListener {

    @BindView(R.id.id_dream_query)
    EditText dreamQuery;
    @BindView(R.id.id_dream_btn)
    Button dreamBtn;
    @BindView(R.id.id_dream_result)
    ListView listView;

    private Context mContext;
    MyAdapter myAdapter;

    SweetAlertDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        ButterKnife.bind(this);
        initEvent();
    }



    private void initEvent() {
        listView.setOnItemClickListener(this);
    }

    @OnClick(R.id.id_dream_btn)
    public void onClick() {
        try {
            String q = dreamQuery.getText().toString();
            presenter.getData(q);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public MvpPresenter initPresenter() {
        return new MvpPresenter(this);
    }

    @Override
    public void setListItem(List data) {
        if (myAdapter == null){
            myAdapter = new MyAdapter(mContext, data);
        }
        if (listView.getAdapter() == null){
            listView.setAdapter(myAdapter);
        }
        myAdapter.refresh(data);
    }

    @Override
    public void showMessage(String messgae) {
        Toast.makeText(mContext, messgae, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showLoading() {
        if (pd == null) {
            pd = new SweetAlertDialog(mContext, SweetAlertDialog.PROGRESS_TYPE);
            pd.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
            pd.setTitleText("Loading");
            pd.setCancelable(true);
        }
        pd.show();
    }

    @Override
    public void hideLoading() {
        pd.hide();
    }

    @Override
    public void onItemClick(AdapterView adapterView, View view, int position, long id) {
        presenter.onItemClick(position);
    }

    @Override
    protected void onDestroy() {
        presenter.destory();
        super.onDestroy();
    }
}

源码地址:https://github.com/Javen205/RxMVP

你可能感兴趣的:(Retrofit2+Rxjava+MVP 实践)