mirror of
https://github.com/WuXiaolong/AndroidMVPSample.git
synced 2025-06-06 13:24:03 +08:00
134 lines
4.0 KiB
Java
134 lines
4.0 KiB
Java
package com.wuxiaolong.androidmvpsample.ui;
|
||
|
||
import android.os.Bundle;
|
||
import android.view.View;
|
||
import android.widget.TextView;
|
||
|
||
import com.wuxiaolong.androidmvpsample.R;
|
||
import com.wuxiaolong.androidmvpsample.mvp.main.MainModel;
|
||
import com.wuxiaolong.androidmvpsample.mvp.main.MainPresenter;
|
||
import com.wuxiaolong.androidmvpsample.mvp.main.MainView;
|
||
import com.wuxiaolong.androidmvpsample.mvp.other.MvpActivity;
|
||
import com.wuxiaolong.androidmvpsample.retrofit.ApiCallback;
|
||
import com.wuxiaolong.androidmvpsample.retrofit.RetrofitCallback;
|
||
|
||
import butterknife.BindView;
|
||
import butterknife.ButterKnife;
|
||
import butterknife.OnClick;
|
||
import retrofit2.Call;
|
||
|
||
/**
|
||
* 由Activity/Fragment实现View里方法,包含一个Presenter的引用
|
||
* Created by WuXiaolong on 2015/9/23.
|
||
* github:https://github.com/WuXiaolong/
|
||
* 微信公众号:吴小龙同学
|
||
* 个人博客:http://wuxiaolong.me/
|
||
*/
|
||
public class MainActivity extends MvpActivity<MainPresenter> implements MainView {
|
||
|
||
@BindView(R.id.text)
|
||
TextView text;
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
setContentView(R.layout.activity_main);
|
||
initToolBarAsHome("MVP+Retrofit+Rxjava");
|
||
}
|
||
|
||
@Override
|
||
protected MainPresenter createPresenter() {
|
||
return new MainPresenter(this);
|
||
}
|
||
|
||
|
||
@Override
|
||
public void getDataSuccess(MainModel model) {
|
||
//接口成功回调
|
||
dataSuccess(model);
|
||
}
|
||
|
||
@Override
|
||
public void getDataFail(String msg) {
|
||
toastShow("网络不给力");
|
||
|
||
}
|
||
|
||
|
||
@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:
|
||
loadDataByRetrofitRxJava();
|
||
break;
|
||
case R.id.button2:
|
||
//请求接口
|
||
mvpPresenter.loadDataByRetrofitRxjava("101310222");
|
||
break;
|
||
}
|
||
}
|
||
|
||
private void loadDataByRetrofit() {
|
||
showProgressDialog();
|
||
Call<MainModel> call = apiStores().loadDataByRetrofit("101190201");
|
||
call.enqueue(new RetrofitCallback<MainModel>() {
|
||
@Override
|
||
public void onSuccess(MainModel model) {
|
||
dataSuccess(model);
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(int code, String msg) {
|
||
toastShow(msg);
|
||
}
|
||
|
||
@Override
|
||
public void onThrowable(Throwable t) {
|
||
toastShow(t.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void onFinish() {
|
||
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) {
|
||
dataSuccess(model);
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(String msg) {
|
||
toastShow(msg);
|
||
}
|
||
|
||
@Override
|
||
public void onFinish() {
|
||
dismissProgressDialog();
|
||
}
|
||
});
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|