java 封装网络,主要是封装的网络请求Retrofit+Rxjava与MVP架构的结合

MVP_Retrofit_RxJava_Example

主要是封装的网络请求Retrofit+Rxjava与MVP架构的结合

效果图如下;

b526c9361ad83070d6af8ac41deab0c3.gif

MVP

5d12f5dc966ec766eb2cee988f2f6355.png

Retrofit Retrofit是Square开发的一个Android和java的REST客户端库。

这两天工作不是很忙,写了一个当前流行的Android MVP+Retrofit(封装)+RxJava实例,mvp和retrofit我就不详细讲的,以后会详细写,下面直接上demo!

1.分类

首先我们要规划好包名和类的分类,不要把类随随便便放,如下图:

382a8d84f31ce3915bcc3ac00acb2225.png

除了上图的模式,我们还可以把所有mvp类放在mvp包下,然后再按照上图写。

2.添加依赖和权限

compile 'com.squareup.retrofit2:retrofit:2.1.0'

compile 'io.reactivex:rxandroid:1.2.1'

compile 'io.reactivex:rxjava:1.1.6'

compile 'com.google.code.gson:gson:2.8.0'

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

3.定义Model--实体类 与URL接口

Model其实就是我们常常写的实体类,一般直接可在AS的GsonFormat插件上生成就可以了.这里我就不贴出来了

URL接口如下:

public interface ApiService {

//baseUrl

String API_SERVER_URL = "http://apistore.baidu.com/microservice/";

//加载天气

@GET("weather")

Observable loadDataByRetrofitRxjava(@Query("citypinyin") String cityId);

// @FormUrlEncoded

// @POST("user/login")

// Observable getlogin(@Field("oper_name") String page, @Field("oper_pwds") String rows);

}

这就需要我们对Retrofit的注解好好去看一看.

5.连接通信(已封装) 其实就是下面的代码,这些我已经封装了,请下载查看。

public static Retrofit retrofit() {

if (mRetrofit == null) {

OkHttpClient.Builder builder = new OkHttpClient.Builder();

OkHttpClient okHttpClient = builder.build();

mRetrofit = new Retrofit.Builder()

.baseUrl(ApiService.API_SERVER_URL)

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.client(okHttpClient)

.build();

}

return mRetrofit;

}

6.View类

public interface WeatherView {

void getWeatherSuccess(WeatherModel weatherModel);

}

下面是activity:

public class MainActivity extends AppCompatActivity implements WeatherView,View.OnClickListener {

private Button btn;

private TextView tv_show;

private EditText edt;

private WeatherPresenter weatherpresenter=new WeatherPresenter(this);

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

tv_show= (TextView) findViewById(R.id.tv_show);

btn= (Button) findViewById(R.id.btn);

edt= (EditText) findViewById(R.id.edt);

btn.setOnClickListener(this);

}

@Override

public void getWeatherSuccess(WeatherModel weatherModel) {

tv_show.setText(" "+weatherModel.getRetData().getWeather()+" "+weatherModel.getRetData().getWD());

}

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.btn:

weatherpresenter.loadDataByRetrofitRxjava11111(edt.getText().toString());

break;

}

}

@Override

protected void onDestroy() {

super.onDestroy();

if (weatherpresenter!=null){

weatherpresenter.detachView();

Log.e("RXJAVA","毁灭");

}

}

7.Presenter类

首先写一个BasePresenter:

public class BasePresenter {

public V mvpView;

protected ApiService apiStores;

private CompositeSubscription mCompositeSubscription;

public void attachView(V mvpView) {

this.mvpView = mvpView;

apiStores = ApiClient.retrofit().create(ApiService.class);

}

public void detachView() {

this.mvpView = null;

onUnsubscribe();

}

//RXjava取消注册,以避免内存泄露

public void onUnsubscribe() {

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));

}

}

下面是WeatherPresenter:

public class WeatherPresenter extends BasePresenter{

public WeatherPresenter(WeatherView view) {

attachView(view);

}

public void loadDataByRetrofitRxjava11111(String cityId) {

addSubscription(apiStores.loadDataByRetrofitRxjava(cityId), new Subscriber() {

@Override

public void onCompleted() {

}

@Override

public void onError(Throwable e) {

}

@Override

public void onNext(WeatherModel weatherModel) {

// Log.e("请求成功","111");

mvpView.getWeatherSuccess(weatherModel);

}

});

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值