public class MainActivity extends Activity {
String url = "http://172.17.29.120/localuser/lujunfang/hello/xiangmuyi_20161008_test1.json";
private List<Car> list;
private MyBaseAdapter myBaseAdapter;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 取出数据
String s = (String) msg.obj;
// 解析json
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
list = bean.list;
// 设置适配器
myBaseAdapter = new MyBaseAdapter(MainActivity.this, list);
listView.setAdapter(myBaseAdapter);
};
};
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
// listView条目点击事件
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
" " + list.get(position).id, 0).show();
}
});
// listView条目长按事件
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// 删除条目
list.remove(position);
// 通知适配器刷新数据
myBaseAdapter.notifyDataSetChanged();
return false;
}
});
HttpUtils httpUtils = new HttpUtils();
/*RequestParams params=new RequestParams();
params.addBodyParameter(name, value)
httpUtils.send(HttpMethod.POST, url, params, callBack);*/
httpUtils.configResponseTextCharset("utf-8");
httpUtils.configCurrentHttpCacheExpiry(0);
// 参数一:请求方式
// 参数二:网络路径
// 参数三:联网请求状态的回调
httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {
@Override
// 请求开始
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
// 请求停止
public void onStopped() {
// TODO Auto-generated method stub
super.onStopped();
}
@Override
// 请求中
public void onLoading(long total, long current, boolean isUploading) {
// TODO Auto-generated method stub
super.onLoading(total, current, isUploading);
}
@Override
// 联网请求失败的回调方法
public void onFailure(HttpException arg0, String arg1) {
}
@Override
// 联网请求成功的回调方法
public void onSuccess(ResponseInfo<String> arg0) {
// 得到网络数据
String result = arg0.result;
System.out.println(result);
// 把网络数据发送到handler里解析
Message msg = Message.obtain();
msg.obj = result;
handler.sendMessage(msg);
}
});
}
}