public class MainActivity extends Activity implements IXListViewListener {
private XListView mXl;
private List<Datas> datas;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件的id
initView();
// 获取数据的方法
getData();
}
/**
* 获取数据的方法
*/
private void getData() {
final String path = "http://www.93.gov.cn/93app/get_search.do?key=九三";
new Thread() {
public void run() {
// 创建HttpClient对象
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
Databean databean = jsonObj(string);
Log.e("tag", databean.toString());
List<Datas> list = databean.getData();
// 进行赋值
datas = list;
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new MyAdapter(MainActivity.this,
datas);
mXl.setAdapter(adapter);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* json解析的方法
*
* @param string
*/
private Databean jsonObj(String string) {
Gson gson = new Gson();
Databean databean = gson.fromJson(string, Databean.class);
return databean;
};
/**
* 初始化控件视图
*/
private void initView() {
mXl = (XListView) findViewById(R.id.xlistview);
// 设置是否能上拉加载
mXl.setPullLoadEnable(true);
// 设置是否能下拉刷新
mXl.setPullRefreshEnable(true);
// 设置监听的方法
mXl.setXListViewListener(this);
}
/**
* 刷新的方法
*/
@Override
public void onRefresh() {
new Thread() {
public void run() {
getData();
};
}.start();
// 关闭的方法
close();
}
/**
* 关闭刷新和加载的方法
*/
private void close() {
mXl.stopLoadMore();
mXl.stopRefresh();
}
/**
* 加载的方法
*/
@Override
public void onLoadMore() {
getDataLoad();
adapter.notifyDataSetChanged();
// 关闭加载
close();
}
/**
* 加载数据的方法
*/
private void getDataLoad() {
final String path = "http://www.93.gov.cn/93app/get_search.do?key=九三";
new Thread() {
public void run() {
// 创建HttpClient对象
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
Databean databean = jsonObj(string);
// Log.e("tag", databean.toString());
List<Datas> list = databean.getData();
datas.addAll(list);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private XListView mXl;
private List<Datas> datas;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件的id
initView();
// 获取数据的方法
getData();
}
/**
* 获取数据的方法
*/
private void getData() {
final String path = "http://www.93.gov.cn/93app/get_search.do?key=九三";
new Thread() {
public void run() {
// 创建HttpClient对象
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
Databean databean = jsonObj(string);
Log.e("tag", databean.toString());
List<Datas> list = databean.getData();
// 进行赋值
datas = list;
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter = new MyAdapter(MainActivity.this,
datas);
mXl.setAdapter(adapter);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
/**
* json解析的方法
*
* @param string
*/
private Databean jsonObj(String string) {
Gson gson = new Gson();
Databean databean = gson.fromJson(string, Databean.class);
return databean;
};
/**
* 初始化控件视图
*/
private void initView() {
mXl = (XListView) findViewById(R.id.xlistview);
// 设置是否能上拉加载
mXl.setPullLoadEnable(true);
// 设置是否能下拉刷新
mXl.setPullRefreshEnable(true);
// 设置监听的方法
mXl.setXListViewListener(this);
}
/**
* 刷新的方法
*/
@Override
public void onRefresh() {
new Thread() {
public void run() {
getData();
};
}.start();
// 关闭的方法
close();
}
/**
* 关闭刷新和加载的方法
*/
private void close() {
mXl.stopLoadMore();
mXl.stopRefresh();
}
/**
* 加载的方法
*/
@Override
public void onLoadMore() {
getDataLoad();
adapter.notifyDataSetChanged();
// 关闭加载
close();
}
/**
* 加载数据的方法
*/
private void getDataLoad() {
final String path = "http://www.93.gov.cn/93app/get_search.do?key=九三";
new Thread() {
public void run() {
// 创建HttpClient对象
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
Databean databean = jsonObj(string);
// Log.e("tag", databean.toString());
List<Datas> list = databean.getData();
datas.addAll(list);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}