1
0
mirror of https://github.com/WuXiaolong/AndroidMVPSample.git synced 2025-06-06 21:34:04 +08:00
This commit is contained in:
xiaomolong 2016-10-12 11:56:40 +08:00
parent f6e95a4eef
commit f0d349d9c4
5 changed files with 88 additions and 24 deletions

View File

@ -17,9 +17,9 @@ public class MainPresenter extends BasePresenter<MainView> {
attachView(view);
}
public void loadData(String cityId) {
public void loadDataByRetrofitRxjava(String cityId) {
mvpView.showLoading();
addSubscription(apiStores.loadData(cityId),
addSubscription(apiStores.loadDataByRetrofitRxjava(cityId),
new ApiCallback<MainModel>() {
@Override
public void onSuccess(MainModel model) {

View File

@ -2,6 +2,7 @@ package com.wuxiaolong.androidmvpsample.retrofit;
import com.wuxiaolong.androidmvpsample.mvp.main.MainModel;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
@ -20,5 +21,9 @@ public interface ApiStores {
//加载天气
@GET("adat/sk/{cityId}.html")
Observable<MainModel> loadData(@Path("cityId") String cityId);
Call<MainModel> loadDataByRetrofit(@Path("cityId") String cityId);
//加载天气
@GET("adat/sk/{cityId}.html")
Observable<MainModel> loadDataByRetrofitRxjava(@Path("cityId") String cityId);
}

View File

@ -16,8 +16,13 @@ import android.widget.Toast;
import com.wuxiaolong.androidmvpsample.R;
import com.wuxiaolong.androidmvpsample.retrofit.ApiStores;
import com.wuxiaolong.androidmvpsample.retrofit.AppClient;
import com.wuxiaolong.androidutils.library.LogUtil;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import retrofit2.Call;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
@ -37,6 +42,7 @@ public class BaseActivity extends AppCompatActivity {
public Activity mActivity;
public ApiStores apiStores = AppClient.retrofit().create(ApiStores.class);
private CompositeSubscription mCompositeSubscription;
private List<Call> calls;
@Override
public void setContentView(@LayoutRes int layoutResID) {
@ -69,17 +75,29 @@ public class BaseActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
callCancel();
onUnsubscribe();
super.onDestroy();
}
public void addCalls(Call call) {
if (calls == null) {
calls = new ArrayList<>();
}
calls.add(call);
}
public void onUnsubscribe() {
if (mCompositeSubscription != null) {
mCompositeSubscription.unsubscribe();//取消注册以避免内存泄露
private void callCancel() {
LogUtil.d("callCancel");
if (calls.size() > 0) {
for (Call call : calls) {
call.cancel();
}
calls.clear();
}
}
public void addSubscription(Observable observable, Subscriber subscriber) {
if (mCompositeSubscription == null) {
mCompositeSubscription = new CompositeSubscription();
@ -97,6 +115,14 @@ public class BaseActivity extends AppCompatActivity {
mCompositeSubscription.add(subscription);
}
public void onUnsubscribe() {
LogUtil.d("onUnsubscribe");
if (mCompositeSubscription != null) {
//取消注册以避免内存泄露
mCompositeSubscription.unsubscribe();
}
}
public Toolbar initToolBar(String title) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

View File

@ -14,6 +14,9 @@ import com.wuxiaolong.androidmvpsample.retrofit.ApiCallback;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* 由Activity/Fragment实现View里方法包含一个Presenter的引用
@ -47,12 +50,7 @@ public class MainActivity extends MvpActivity<MainPresenter> implements MainView
@Override
public void getDataSuccess(MainModel model) {
//接口成功回调
MainModel.WeatherinfoBean weatherinfo = model.getWeatherinfo();
String showData = getResources().getString(R.string.city) + weatherinfo.getCity()
+ getResources().getString(R.string.wd) + weatherinfo.getWD()
+ getResources().getString(R.string.ws) + weatherinfo.getWS()
+ getResources().getString(R.string.time) + weatherinfo.getTime();
text.setText(showData);
dataSuccess(model);
}
@Override
@ -72,32 +70,49 @@ public class MainActivity extends MvpActivity<MainPresenter> implements MainView
}
@OnClick({R.id.button1, R.id.button2})
@OnClick({R.id.button0, R.id.button1, R.id.button2})
public void onClick(View view) {
switch (view.getId()) {
case R.id.button0:
loadDataByRetrofit();
break;
case R.id.button1:
loadData();
loadDataByRetrofitRxjava();
break;
case R.id.button2:
//请求接口
mvpPresenter.loadData("101310222");
mvpPresenter.loadDataByRetrofitRxjava("101310222");
break;
}
}
//全国+国外主要城市代码http://mobile.weather.com.cn/js/citylist.xml
private void loadData() {
private void loadDataByRetrofit() {
showProgressDialog();
addSubscription(apiStores.loadData("101190201"),
Call<MainModel> call = apiStores.loadDataByRetrofit("101190201");
call.enqueue(new Callback<MainModel>() {
@Override
public void onResponse(Call<MainModel> call, Response<MainModel> response) {
dataSuccess(response.body());
dismissProgressDialog();
}
@Override
public void onFailure(Call<MainModel> call, Throwable t) {
toastShow(t.getMessage());
dismissProgressDialog();
}
});
addCalls(call);
}
//全国+国外主要城市代码http://mobile.weather.com.cn/js/citylist.xml
private void loadDataByRetrofitRxjava() {
showProgressDialog();
addSubscription(apiStores.loadDataByRetrofitRxjava("101220602"),
new ApiCallback<MainModel>() {
@Override
public void onSuccess(MainModel model) {
MainModel.WeatherinfoBean weatherinfo = model.getWeatherinfo();
String showData = getResources().getString(R.string.city) + weatherinfo.getCity()
+ getResources().getString(R.string.wd) + weatherinfo.getWD()
+ getResources().getString(R.string.ws) + weatherinfo.getWS()
+ getResources().getString(R.string.time) + weatherinfo.getTime();
text.setText(showData);
dataSuccess(model);
}
@Override
@ -112,4 +127,13 @@ public class MainActivity extends MvpActivity<MainPresenter> implements MainView
}
});
}
private void dataSuccess(MainModel model) {
MainModel.WeatherinfoBean weatherinfo = model.getWeatherinfo();
String showData = getResources().getString(R.string.city) + weatherinfo.getCity()
+ getResources().getString(R.string.wd) + weatherinfo.getWD()
+ getResources().getString(R.string.ws) + weatherinfo.getWS()
+ getResources().getString(R.string.time) + weatherinfo.getTime();
text.setText(showData);
}
}

View File

@ -8,6 +8,15 @@
<include layout="@layout/toolbar"/>
<Button
android:id="@+id/button0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="普通写法(Retrofit)"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"