import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import com.baway.test.MyAdapter.MyAdapter;
import com.baway.test.bean.Dx;
import com.baway.test.bean.Result;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
List<Result> list1=(List<Result>) msg.obj;
adapter = new MyAdapter(MainActivity.this,list1);
pl.setAdapter(adapter);
adapter.notifyDataSetChanged();
pl.onRefreshComplete();
}
};
private List<Result> li;
private MyAdapter adapter;
private PullToRefreshListView pl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pl = (PullToRefreshListView) findViewById(R.id.pl);
pl.setMode(Mode.BOTH);
initData();
pl.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
initData();
}
});
}
//申请数据
protected void initData() {
new Thread(){
public void run() {
try {
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet("http://v.juhe.cn/gerenshebao/test/getcities.php?key=ad6daef945cdd9aab38bbacefd9f22f8");
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
Gson gson=new Gson();
Dx fromJson = gson.fromJson(content, Dx.class);
List<Result> result = fromJson.getResult();
Message msg = Message.obtain();
msg.obj=result;
// msg.what=a;
handler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import com.baway.test.MyAdapter.MyAdapter;
import com.baway.test.bean.Dx;
import com.baway.test.bean.Result;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
List<Result> list1=(List<Result>) msg.obj;
adapter = new MyAdapter(MainActivity.this,list1);
pl.setAdapter(adapter);
adapter.notifyDataSetChanged();
pl.onRefreshComplete();
}
};
private List<Result> li;
private MyAdapter adapter;
private PullToRefreshListView pl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pl = (PullToRefreshListView) findViewById(R.id.pl);
pl.setMode(Mode.BOTH);
initData();
pl.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
initData();
}
});
}
//申请数据
protected void initData() {
new Thread(){
public void run() {
try {
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet("http://v.juhe.cn/gerenshebao/test/getcities.php?key=ad6daef945cdd9aab38bbacefd9f22f8");
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
Gson gson=new Gson();
Dx fromJson = gson.fromJson(content, Dx.class);
List<Result> result = fromJson.getResult();
Message msg = Message.obtain();
msg.obj=result;
// msg.what=a;
handler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}