mirror of
https://github.com/WuXiaolong/AndroidMVPSample.git
synced 2025-12-20 10:35:41 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.wuxiaolong.androidmvpsample.model;
|
||||
|
||||
/**
|
||||
* Created by WuXiaolong on 2015/9/23.
|
||||
*/
|
||||
public class MainModel {
|
||||
String city;
|
||||
String wd;
|
||||
String ws;
|
||||
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,58 @@
|
||||
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.view.MainView;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import cz.msebera.android.httpclient.Header;
|
||||
|
||||
/**
|
||||
* Created by WuXiaolong on 2015/9/23.
|
||||
*/
|
||||
public class MainPresenter implements Presenter<MainView> {
|
||||
private MainView mMainView;
|
||||
|
||||
@Override
|
||||
public void attachView(MainView 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
|
||||
public void detachView() {
|
||||
this.mMainView = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.wuxiaolong.androidmvpsample.presenter;
|
||||
|
||||
public interface Presenter<V> {
|
||||
|
||||
void attachView(V view);
|
||||
|
||||
void detachView();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.wuxiaolong.androidmvpsample.ui;
|
||||
|
||||
import android.os.Bundle;
|
||||
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.presenter.MainPresenter;
|
||||
import com.wuxiaolong.androidmvpsample.view.MainView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements MainView {
|
||||
ProgressBar mProgressBar;
|
||||
TextView text;
|
||||
MainPresenter mMainPresenter;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
initView();
|
||||
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
text = (TextView) findViewById(R.id.text);
|
||||
mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
|
||||
mMainPresenter = new MainPresenter();
|
||||
mMainPresenter.attachView(this);
|
||||
mMainPresenter.loadData();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
mMainPresenter.detachView();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showData(MainModel mainModel) {
|
||||
text.setText("城市:" + mainModel.getCity()
|
||||
+ "\n风向:" + mainModel.getWd()
|
||||
+ "\n风级:" + mainModel.getWs()
|
||||
+ "\n发布时间:" + mainModel.getTime());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void showProgress() {
|
||||
mProgressBar.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hideProgress() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.wuxiaolong.androidmvpsample.view;
|
||||
|
||||
import com.wuxiaolong.androidmvpsample.model.MainModel;
|
||||
|
||||
/**
|
||||
* Created by WuXiaolong on 2015/9/23.
|
||||
*/
|
||||
public interface MainView {
|
||||
void showData(MainModel mainModel);
|
||||
void showProgress();
|
||||
void hideProgress();
|
||||
}
|
||||
Reference in New Issue
Block a user