1
0
mirror of https://github.com/WuXiaolong/AndroidMVPSample.git synced 2025-06-06 21:34:04 +08:00

MainModelBean

This commit is contained in:
WuXiaolong 2016-01-18 11:11:36 +08:00
parent 67dd043adf
commit c2daf80356
9 changed files with 148 additions and 109 deletions

2
.idea/misc.xml generated
View File

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -1,43 +1,52 @@
package com.wuxiaolong.androidmvpsample.model;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.wuxiaolong.androidmvpsample.presenter.IMainPresenter;
import org.json.JSONException;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
/**
* Created by WuXiaolong on 2015/9/23.
* 业务具体处理包括负责存储检索操纵数据等
*/
public class MainModel {
String city;
String wd;
String ws;
String time;
IMainPresenter mIMainPresenter;
public String getCity() {
return city;
public MainModel(IMainPresenter iMainPresenter) {
this.mIMainPresenter = iMainPresenter;
}
public void setCity(String city) {
this.city = city;
public void loadData() {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get("http://www.weather.com.cn/adat/sk/101010100.html", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
try {
MainModelBean mainModelBean = new MainModelBean();
JSONObject weatherinfo = response.getJSONObject("weatherinfo");
mainModelBean.setCity(weatherinfo.getString("city"));
mainModelBean.setWd(weatherinfo.getString("WD"));
mainModelBean.setWs(weatherinfo.getString("WS"));
mainModelBean.setTime(weatherinfo.getString("time"));
mIMainPresenter.loadDataSuccess(mainModelBean);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
mIMainPresenter.loadDataFailure();
}
});
}
public String getWd() {
return wd;
}
public void setWd(String wd) {
this.wd = wd;
}
public String getWs() {
return ws;
}
public void setWs(String ws) {
this.ws = ws;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}

View File

@ -0,0 +1,43 @@
package com.wuxiaolong.androidmvpsample.model;
/**
* Created by WuXiaolong on 2015/9/23.
*/
public class MainModelBean {
private String city;
private String wd;
private String ws;
private String time;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getWd() {
return wd;
}
public void setWd(String wd) {
this.wd = wd;
}
public String getWs() {
return ws;
}
public void setWs(String ws) {
this.ws = ws;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}

View File

@ -0,0 +1,13 @@
package com.wuxiaolong.androidmvpsample.presenter;
import com.wuxiaolong.androidmvpsample.model.MainModelBean;
/**
* Created by WuXiaolong on 2015/9/23.
* 此接口作用是连接Model
*/
public interface IMainPresenter {
void loadDataSuccess(MainModelBean mainModelBean);
void loadDataFailure();
}

View File

@ -1,24 +1,20 @@
package com.wuxiaolong.androidmvpsample.presenter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.wuxiaolong.androidmvpsample.model.MainModel;
import com.wuxiaolong.androidmvpsample.model.MainModelBean;
import com.wuxiaolong.androidmvpsample.view.MainView;
import org.json.JSONException;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
/**
* Created by WuXiaolong on 2015/9/23.
* 业务具体处理
* View和Model的桥梁它从Model层检索数据后返回给View层
*/
public class MainPresenter implements Presenter<MainView> {
public class MainPresenter implements Presenter<MainView>, IMainPresenter {
private MainView mMainView;
private MainModel mMainModel;
public MainPresenter(MainView view) {
attachView(view);
mMainModel = new MainModel(this);
}
@Override
@ -26,38 +22,25 @@ public class MainPresenter implements Presenter<MainView> {
this.mMainView = view;
}
public void loadData() {
mMainView.showProgress();
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get("http://www.weather.com.cn/adat/sk/101010100.html", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
try {
MainModel mainModel = new MainModel();
JSONObject weatherinfo = response.getJSONObject("weatherinfo");
mainModel.setCity(weatherinfo.getString("city"));
mainModel.setWd(weatherinfo.getString("WD"));
mainModel.setWs(weatherinfo.getString("WS"));
mainModel.setTime(weatherinfo.getString("time"));
mMainView.showData(mainModel);
mMainView.hideProgress();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
mMainView.hideProgress();
}
});
}
@Override
public void detachView() {
this.mMainView = null;
}
public void loadData() {
mMainView.showProgress();
mMainModel.loadData();
}
@Override
public void loadDataSuccess(MainModelBean mainModelBean) {
mMainView.showData(mainModelBean);
mMainView.hideProgress();
}
@Override
public void loadDataFailure() {
mMainView.hideProgress();
}
}

View File

@ -1,22 +1,25 @@
package com.wuxiaolong.androidmvpsample.ui;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.wuxiaolong.androidmvpsample.R;
import com.wuxiaolong.androidmvpsample.model.MainModel;
import com.wuxiaolong.androidmvpsample.model.MainModelBean;
import com.wuxiaolong.androidmvpsample.presenter.MainPresenter;
import com.wuxiaolong.androidmvpsample.view.MainView;
/**
* Created by WuXiaolong on 2015/9/23.
* 由Activity/Fragment实现View里方法包含一个Presenter的引用
*/
public class MainActivity extends AppCompatActivity implements MainView {
ProgressBar mProgressBar;
TextView text;
MainPresenter mMainPresenter;
private ProgressBar mProgressBar;
private TextView text;
private MainPresenter mMainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -30,7 +33,14 @@ public class MainActivity extends AppCompatActivity implements MainView {
text = (TextView) findViewById(R.id.text);
mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
mMainPresenter = new MainPresenter(this);
mMainPresenter.loadData();
//制造延迟效果
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mMainPresenter.loadData();
}
}, 2000);
}
@Override
@ -40,11 +50,12 @@ public class MainActivity extends AppCompatActivity implements MainView {
}
@Override
public void showData(MainModel mainModel) {
text.setText("城市:" + mainModel.getCity()
+ "\n风向" + mainModel.getWd()
+ "\n风级" + mainModel.getWs()
+ "\n发布时间" + mainModel.getTime());
public void showData(MainModelBean mainModelBean) {
String showData = getResources().getString(R.string.city) + mainModelBean.getCity()
+ getResources().getString(R.string.wd) + mainModelBean.getWd()
+ getResources().getString(R.string.ws) + mainModelBean.getWs()
+ getResources().getString(R.string.time) + mainModelBean.getTime();
text.setText(showData);
}
@ -58,27 +69,5 @@ public class MainActivity extends AppCompatActivity implements MainView {
mProgressBar.setVisibility(View.GONE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -1,13 +1,13 @@
package com.wuxiaolong.androidmvpsample.view;
import com.wuxiaolong.androidmvpsample.model.MainModel;
import com.wuxiaolong.androidmvpsample.model.MainModelBean;
/**
* Created by WuXiaolong on 2015/9/23.
* 处理业务需要哪些方法
*/
public interface MainView {
void showData(MainModel mainModel);
void showData(MainModelBean mainModelBean);
void showProgress();

View File

@ -11,8 +11,7 @@
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/mProgressBar"

View File

@ -1,6 +1,9 @@
<resources>
<string name="app_name">AndroidMVPSample</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="city">城市:</string>
<string name="wd">\n风向:</string>
<string name="ws">\n风级:</string>
<string name="time">\n发布时间:</string>
</resources>