mirror of
https://github.com/WuXiaolong/AndroidMVPSample.git
synced 2025-06-07 13:54:04 +08:00
MainModelBean
This commit is contained in:
parent
67dd043adf
commit
c2daf80356
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -37,7 +37,7 @@
|
|||||||
<ConfirmationsSetting value="0" id="Add" />
|
<ConfirmationsSetting value="0" id="Add" />
|
||||||
<ConfirmationsSetting value="0" id="Remove" />
|
<ConfirmationsSetting value="0" id="Remove" />
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
@ -1,43 +1,52 @@
|
|||||||
package com.wuxiaolong.androidmvpsample.model;
|
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.
|
* Created by WuXiaolong on 2015/9/23.
|
||||||
|
* 业务具体处理,包括负责存储、检索、操纵数据等
|
||||||
*/
|
*/
|
||||||
public class MainModel {
|
public class MainModel {
|
||||||
String city;
|
IMainPresenter mIMainPresenter;
|
||||||
String wd;
|
|
||||||
String ws;
|
|
||||||
String time;
|
|
||||||
|
|
||||||
public String getCity() {
|
public MainModel(IMainPresenter iMainPresenter) {
|
||||||
return city;
|
this.mIMainPresenter = iMainPresenter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCity(String city) {
|
public void loadData() {
|
||||||
this.city = city;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -1,24 +1,20 @@
|
|||||||
package com.wuxiaolong.androidmvpsample.presenter;
|
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.MainModel;
|
||||||
|
import com.wuxiaolong.androidmvpsample.model.MainModelBean;
|
||||||
import com.wuxiaolong.androidmvpsample.view.MainView;
|
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.
|
* 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 MainView mMainView;
|
||||||
|
private MainModel mMainModel;
|
||||||
|
|
||||||
public MainPresenter(MainView view) {
|
public MainPresenter(MainView view) {
|
||||||
attachView(view);
|
attachView(view);
|
||||||
|
mMainModel = new MainModel(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -26,38 +22,25 @@ public class MainPresenter implements Presenter<MainView> {
|
|||||||
this.mMainView = view;
|
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
|
@Override
|
||||||
public void detachView() {
|
public void detachView() {
|
||||||
this.mMainView = null;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,25 @@
|
|||||||
package com.wuxiaolong.androidmvpsample.ui;
|
package com.wuxiaolong.androidmvpsample.ui;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.wuxiaolong.androidmvpsample.R;
|
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.presenter.MainPresenter;
|
||||||
import com.wuxiaolong.androidmvpsample.view.MainView;
|
import com.wuxiaolong.androidmvpsample.view.MainView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by WuXiaolong on 2015/9/23.
|
||||||
|
* 由Activity/Fragment实现View里方法,包含一个Presenter的引用
|
||||||
|
*/
|
||||||
public class MainActivity extends AppCompatActivity implements MainView {
|
public class MainActivity extends AppCompatActivity implements MainView {
|
||||||
ProgressBar mProgressBar;
|
private ProgressBar mProgressBar;
|
||||||
TextView text;
|
private TextView text;
|
||||||
MainPresenter mMainPresenter;
|
private MainPresenter mMainPresenter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -30,7 +33,14 @@ public class MainActivity extends AppCompatActivity implements MainView {
|
|||||||
text = (TextView) findViewById(R.id.text);
|
text = (TextView) findViewById(R.id.text);
|
||||||
mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
|
mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
|
||||||
mMainPresenter = new MainPresenter(this);
|
mMainPresenter = new MainPresenter(this);
|
||||||
mMainPresenter.loadData();
|
//制造延迟效果
|
||||||
|
new Handler().postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mMainPresenter.loadData();
|
||||||
|
}
|
||||||
|
}, 2000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -40,11 +50,12 @@ public class MainActivity extends AppCompatActivity implements MainView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showData(MainModel mainModel) {
|
public void showData(MainModelBean mainModelBean) {
|
||||||
text.setText("城市:" + mainModel.getCity()
|
String showData = getResources().getString(R.string.city) + mainModelBean.getCity()
|
||||||
+ "\n风向:" + mainModel.getWd()
|
+ getResources().getString(R.string.wd) + mainModelBean.getWd()
|
||||||
+ "\n风级:" + mainModel.getWs()
|
+ getResources().getString(R.string.ws) + mainModelBean.getWs()
|
||||||
+ "\n发布时间:" + mainModel.getTime());
|
+ 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);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.wuxiaolong.androidmvpsample.view;
|
package com.wuxiaolong.androidmvpsample.view;
|
||||||
|
|
||||||
import com.wuxiaolong.androidmvpsample.model.MainModel;
|
import com.wuxiaolong.androidmvpsample.model.MainModelBean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by WuXiaolong on 2015/9/23.
|
* Created by WuXiaolong on 2015/9/23.
|
||||||
* 处理业务需要哪些方法
|
* 处理业务需要哪些方法
|
||||||
*/
|
*/
|
||||||
public interface MainView {
|
public interface MainView {
|
||||||
void showData(MainModel mainModel);
|
void showData(MainModelBean mainModelBean);
|
||||||
|
|
||||||
void showProgress();
|
void showProgress();
|
||||||
|
|
||||||
|
@ -11,8 +11,7 @@
|
|||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/text"
|
android:id="@+id/text"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content" />
|
||||||
android:text="@string/hello_world" />
|
|
||||||
|
|
||||||
<ProgressBar
|
<ProgressBar
|
||||||
android:id="@+id/mProgressBar"
|
android:id="@+id/mProgressBar"
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">AndroidMVPSample</string>
|
<string name="app_name">AndroidMVPSample</string>
|
||||||
|
|
||||||
<string name="hello_world">Hello world!</string>
|
<string name="hello_world">Hello world!</string>
|
||||||
<string name="action_settings">Settings</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>
|
</resources>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user