Retrofit+RxJava+MVP学习笔记
2016-06-26 15:15
417 查看
一、Retrofit:
1、对于统一请求参数的封装(封装到请求头中)
2、实体类解析
(1)实体基类
3、网络请求管理类:
二、RxJava
1、对于返回值的统一处理
(1)处理完成后的回调接口
三、MVP:
1、BasePresent:model和vi
4000
ew交互的桥梁
三、简单登录页面demo:
1、LoginContract便于管理model,view,present的接口
3、LoginModel:数据请求的操作:
4、LoginPresent:
学习推荐:
Rxjava:http://gank.io/post/560e15be2dca930e00da1083
MVP:http://www.jianshu.com/p/b49958e1889d
http://wuxiaolong.me/2016/06/12/mvpRetrofitRxjava/
1、对于统一请求参数的封装(封装到请求头中)
public class BaseIntercepter implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { //加入请求头的信息 Request request = chain.request(); request.newBuilder().addHeader("token","xxxx").build(); Response response = chain.proceed(request); return response; } }
2、实体类解析
(1)实体基类
public class BaseEntity<T>{ // 接口请求返回带状态码 public int code; // 服务器返回的提示信息 public String tip; //data数据 public T data; }
3、网络请求管理类:
public class BaseHttpMethod { private static BaseHttpMethod instance; private Context mContext; private Retrofit retrofit; private static final String baseUrl = "http://101.200.xxx.xx/api/"; //手动创建一个OkHttpClient private OkHttpClient okHttpClient(){ //设置log显示 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); return new OkHttpClient.Builder() .retryOnConnectionFailure(true) //失败重连 .readTimeout(60*1000, TimeUnit.MILLISECONDS) .addNetworkInterceptor(logging) //拦截器 .addInterceptor(new BaseIntercepter()) .build(); } public BaseHttpMethod(){ retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient()) .build(); } public static BaseHttpMethod getInstance(){ if (instance==null){ synchronized (BaseHttpMethod.class){ instance = new BaseHttpMethod(); } } return instance; } public <T> T create(Class<T> service){ return retrofit.create(service); } }
二、RxJava
1、对于返回值的统一处理
(1)处理完成后的回调接口
public interface ApiCallBack<T> { void onSuccess(T model); void onFailure(); void onCompleted(); }(2)进行统一处理:
public class SubscriberCallBack<T> extends Subscriber<T> { private ApiCallBack<T> apiCallBack; //回调监听 public SubscriberCallBack(ApiCallBack<T> apiCallBack) { this.apiCallBack = apiCallBack; } @Override public void onCompleted() { apiCallBack.onCompleted(); } @Override public void onError(Throwable e) { e.printStackTrace(); apiCallBack.onFailure(); } @Override public void onNext(T t) { if (t instanceof BaseEntity){ BaseEntity<T> entity = (BaseEntity<T>) t; if (entity.code==1000){ Log.e("aa","成功了"); } } apiCallBack.onSuccess(t); } }
三、MVP:
1、BasePresent:model和vi
4000
ew交互的桥梁
public class BasePresenter { private CompositeSubscription mCompositeSubscription; private BaseView view; public BasePresenter(){ } public BasePresenter(BaseView view){ this.view = view; } //Rxjava取消注册,避免内存泄露 public void onUnsubscribe(){ view = null; if (mCompositeSubscription!=null&&mCompositeSubscription.hasSubscriptions()){ mCompositeSubscription.unsubscribe(); } } public void addSubscription(Observable observable, Subscriber subscriber){ if (mCompositeSubscription==null){ mCompositeSubscription = new CompositeSubscription(); } mCompositeSubscription.add(observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber)); } }
三、简单登录页面demo:
1、LoginContract便于管理model,view,present的接口
public interface LoginContract { abstract class Presenter extends BasePresenter{ //登陆 abstract void login(String mobile,String code); //发送验证码 abstract void sendVifycode(String moble); } interface View extends BaseView{ void showSendVifyToast(BaseEntity<String> baseEntity); void loginSuccess(BaseEntity<UserEntity> userEntity); } //封装网络请求 interface Model { @GET("user/vercodeLogin.go?") Observable<BaseEntity<UserEntity>> login(@Query("mobileNo") String phone,@Query("vercode") String code,@Query("appType") int type); @GET("user/sendVerCode.go?") Observable<BaseEntity<String>> sendVifyCode(@Query("mobileNo") String phone); } }2、LoginActivity:将网络请求需要的数据传给present,将present返回的数据做页面显示处理。
public class LoginActivity extends Activity implements LoginContract.View, View.OnClickListener { private LoginContract.Presenter mPresenter; private EditText mobileText; private EditText vercodeText; private Button submitBtm; private Button loginBtm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mPresenter = new LoginPresenter(this); mobileText = (EditText) findViewById(R.id.id_login_mobilno_value); vercodeText = (EditText) findViewById(R.id.id_login_vercode_value); submitBtm = (Button) findViewById(R.id.id_login_vercode_btn); loginBtm = (Button) findViewById(R.id.id_login_btn); submitBtm.setOnClickListener(this); loginBtm.setOnClickListener(this); } //发送验证码后的页面处理 @Override public void showSendVifyToast(BaseEntity<String> baseEntity) { } //登录成功 @Override public void loginSuccess(BaseEntity<UserEntity> userEntity) { } @Override public void onClick(View v) { switch (v.getId()) { case R.id.id_login_vercode_btn: mPresenter.sendVifycode(mobileText.getText().toString()); break; case R.id.id_login_btn: mPresenter.login(mobileText.getText().toString(),vercodeText.getText().toString()); break; default: break; } } }
3、LoginModel:数据请求的操作:
public class LoginModel extends BaseModel implements LoginContract.Model { LoginContract.Model service = BaseHttpMethod.getInstance().create(LoginContract.Model.class); @Override public Observable<BaseEntity<UserEntity>> login(String phone,String code,int type) { return service.login(phone,code,type); } @Override public Observable<BaseEntity<String>> sendVifyCode(String phone) { return service.sendVifyCode(phone); } }
4、LoginPresent:
public class LoginPresenter extends LoginContract.Presenter{ private LoginContract.View mView; private LoginModel loginModel; public LoginPresenter(LoginContract.View view){ this.mView = view; loginModel = new LoginModel(); } @Override void login(String mobile, String code) { addSubscription(loginModel.login(mobile,code,0),new SubscriberCallBack(new ApiCallBack<BaseEntity<UserEntity>>() { @Override public void onSuccess(BaseEntity<UserEntity> model) { //登录成功 mView.loginSuccess(model); } @Override public void onFailure() { } @Override public void onCompleted() { } })); } @Override public void sendVifycode(String moble) { addSubscription(loginModel.sendVifyCode(moble),new Subscriber<BaseEntity<String>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(BaseEntity<String> baseEntity) { mView.showSendVifyToast(baseEntity); } }); } }
学习推荐:
Rxjava:http://gank.io/post/560e15be2dca930e00da1083
MVP:http://www.jianshu.com/p/b49958e1889d
http://wuxiaolong.me/2016/06/12/mvpRetrofitRxjava/
相关文章推荐
- 关于java适配器模式的理解
- 启动eclipse报错failed to load the JNI shared library
- Spring学习-第1天
- java compiler level does not match the version of the installed java project
- Java设计模式——模板模式
- Java多线程
- java 通过Executors.newFixedThreadPool创建固定大小的线程执行器
- Java中只有值传递,没有引用传递 !
- java 解析json字符串
- Java 修饰符
- jcrop+java 后台
- 《Java JD7 学习笔记》课后练习题1
- elasticsearch class file for java.lang.AutoCloseable not found
- Maven手动添加dependency(以Oracle JDBC为例)
- struts2 环境建立(1)
- JAVA笔记-基本数据的参数查找优先级(2016年更新)
- 构建自己的Java Web框架(一)之前言
- Java基础与案例开发详解のjava语言基础入门
- Junit4 -Java单元测试框架使用的例子
- <hostname>:9000 failed on connection exception: java.net.ConnectException: 拒绝连接