上拉加载
**“`
public class MainActivity extends Activity {
protected static final String path = "http://v.juhe.cn/weixin/query";
protected static final String key = "0917c812f187b92e025c9b7c2d4e59b5";
private EditText pno;
private ListView lv;
private EditText item;
private ProgressDialog dialog;
private ArrayList<Rlist> lists=new ArrayList<WeiXinInfo.Result.Rlist>();
Handler hand=new Handler(){
MyAdapter adapter;
public void handleMessage(android.os.Message msg) {
//把gson解析的代码传过来
WeiXinInfo info=(WeiXinInfo) msg.obj;
//把查询到的数据放入集合中,当你每次加载数据时,都会使用这个lists集合,把新的数据加载进去,适配器也是使用这个lists集合中数据
lists.addAll(info.result.list);
//取消加载对话框
dialog.dismiss();
//当一次加载时adapter对象为空
if(adapter==null){
//创建adapter对象,自定的适配器
adapter = new MyAdapter(MainActivity.this, lists);
//设置适配器
lv.setAdapter(adapter);
}else{
//当不是第一次加载时,刷新列表
adapter.notifyDataSetChanged();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
findComp();
}
private String pn;
private String it;
//Http-client get请求
private void getData_Get(final String pno,final String item){
//调用该方法时,创建加载对话框
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.show();
this.pn=pno;
this.it=item;
new Thread(){
public void run() {
String getPath=path+"?key="+key+"&pno="+pno+"&ps="+item;
//定义客户端对象
HttpClient client=new DefaultHttpClient();
//定义一个get对象
HttpGet get=new HttpGet(getPath);
//执行请求
try {
HttpResponse httpResponse = client.execute(get);
//响应码 获取状态行
if(httpResponse.getStatusLine().getStatusCode()==200){
//实体对象
HttpEntity entity = httpResponse.getEntity();
//获取实体内容
InputStream is = entity.getContent();
int len;
byte[] arr=new byte[1024];
ByteArrayOutputStream aos=new ByteArrayOutputStream();
while((len=is.read(arr))!=-1){
aos.write(arr, 0, len);
}
Gson g=new Gson();
final WeiXinInfo info=g.fromJson(aos.toString(), WeiXinInfo.class);
System.out.println(aos.toString());
hand.obtainMessage(0, info).sendToTarget();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
//post请求
private void getData_Post(final String pno,final String item) {
// TODO Auto-generated method stub
dialog=new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.show();
this.pn=pno;
this.it=item;
new Thread(){
public void run() {
//创建创建客户端对象
HttpClient client=new DefaultHttpClient();
//创建post对象
HttpPost post=new HttpPost(path);
//创建一个放实体键值对的集合
List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("key", key));
list.add(new BasicNameValuePair("pno", pno));
list.add(new BasicNameValuePair("ps", item ));
try {
//创建一个url编码过的实体对象
HttpEntity entity = new UrlEncodedFormEntity(list);
//发给服务器的内容
post.setEntity(entity);
//发送请求
HttpResponse execute = client.execute(post);
if(execute.getStatusLine().getStatusCode()==200){
//获取实体对象中的实体内容
InputStream is = execute.getEntity().getContent();
int len;
byte[] arr=new byte[1024];
ByteArrayOutputStream aos=new ByteArrayOutputStream();
while((len=is.read(arr))!=-1){
aos.write(arr, 0, len);
}
System.out.println("************************************");
System.out.println(aos.toString());
Gson g=new Gson();
WeiXinInfo info=g.fromJson(aos.toString(), WeiXinInfo.class);
hand.obtainMessage(1, info).sendToTarget();
}
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
//点击按钮时,触发该方法
public void get(View view){
getData_Get(pno.getText().toString().trim(), item.getText().toString().trim());
}
//点击按钮时,触发该方法
public void post(View view){
System.out.println("--------------------------------");
getData_Post(pno.getText().toString().trim(), item.getText().toString().trim());
}
private void findComp() {
item = (EditText) findViewById(R.id.e_item);
pno = (EditText) findViewById(R.id.e_pno);
lv = (ListView) findViewById(R.id.l_lv);
//适配listview列表
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int lastVisiblePosition = view.getLastVisiblePosition();
if(lastVisiblePosition==lists.size()-1&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
int parseInt = Integer.parseInt(pn);
parseInt++;
getData_Get(parseInt+"", it);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
“`**