package com.example.liangmignhui20160505;
import java.io.IOException;
import java.util.ArrayList;
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.text.format.DateUtils;
import android.util.Log;
import android.widget.ListView;
import com.example.adapter.MybaseTwo;
import com.example.vo.Data;
import com.example.vo.Myinfo;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
public class SucceActivity extends Activity{
private PullToRefreshListView homelist_view;
private List<Myinfo> rows=new ArrayList<Myinfo>();
int j=10;
int i=10;
String str;
private MybaseTwo base;
private String url="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0";
Handler han=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
msg.obj=str;
Gson g=new Gson();
Data data=g.fromJson(str,Data.class);
rows=data.getResult().getRows();
base=new MybaseTwo(getApplicationContext(), rows);
homelist_view.setAdapter(base);
break;
case 2:
msg.obj=str;
Gson g1=new Gson();
Data data1=g1.fromJson(str,Data.class);
if(data1!=null){
rows=data1.getResult().getRows();
base=new MybaseTwo(getApplicationContext(), rows);
homelist_view.setAdapter(base);
base.notifyDataSetChanged();
homelist_view.onRefreshComplete();
}
break;
case 3:
msg.obj=str;
Gson g2=new Gson();
Data data2=g2.fromJson(str,Data.class);
if(data2!=null){
List<Myinfo> info=data2.getResult().getRows();
rows.addAll(info);
base.notifyDataSetChanged();
homelist_view.onRefreshComplete();
}
break;
default:
break;
}
};
};
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
homelist_view=(PullToRefreshListView) findViewById(R.id.homelist_view);
homelist_view.setMode(Mode.BOTH);
new Thread(){
public void run() {
init();
}
}.start();
homelist_view.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
String label = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
new Thread(){
@Override
public void run() {
try {
j++;
init2(url+j);
han.sendMessage(han.obtainMessage(2, str));
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//刷新出新数据
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run() {
try {
i--;
init2(url+i);
han.sendMessage(han.obtainMessage(3, str));
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
});
}
private void init() {
// TODO Auto-generated method stub
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
try {
HttpResponse httpResponse=httpClient.execute(httpGet);
int len=httpResponse.getStatusLine().getStatusCode();
if(len==200){
HttpEntity httpEntity=httpResponse.getEntity();
str=EntityUtils.toString(httpEntity, "utf-8");
Gson g=new Gson();
Data data=g.fromJson(str,Data.class);
rows=data.getResult().getRows();
// han.sendMessage(han.obtainMessage(1, str));
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
private void init2(String url) {
// TODO Auto-generated method stub
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
try {
HttpResponse httpResponse=httpClient.execute(httpGet);
int len=httpResponse.getStatusLine().getStatusCode();
if(len==200){
HttpEntity httpEntity=httpResponse.getEntity();
str=EntityUtils.toString(httpEntity, "utf-8");
Gson g=new Gson();
Data data=g.fromJson(str,Data.class);
rows=data.getResult().getRows();
han.sendMessage(han.obtainMessage(1, str));
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/homelist_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrDrawable="@drawable/ic_launcher"
ptr:ptrMode="both" />
</LinearLayout>
package com.example.vo;
public class Data {
private Resulto result;
public Resulto getResult() {
return result;
}
public void setResult(Resulto result) {
this.result = result;
}
}
package com.example.vo;
public class House {
private String address;
private String default_image;
private String loupan_name;
private String tags;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDefault_image() {
return default_image;
}
public void setDefault_image(String default_image) {
this.default_image = default_image;
}
public String getLoupan_name() {
return loupan_name;
}
public void setLoupan_name(String loupan_name) {
this.loupan_name = loupan_name;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
}
package com.example.vo;
public class Myinfo {
private House info;
public House getInfo() {
return info;
}
public void setInfo(House info) {
this.info = info;
}
}
package com.example.vo;
import java.util.List;
public class Resulto {
private List<Myinfo> rows;
public List<Myinfo> getRows() {
return rows;
}
public void setRows(List<Myinfo> rows) {
this.rows = rows;
}
}