import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.ListView;
import com.example.adapter.Myadapter;
import com.example.bean.Search;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private ProgressDialog dialog;
private List<Search> list;
private String pname;
private Search se;
private InputStream in;
private String url;
private int rn=10;
private PullToRefreshListView pull_refresh_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ClassTaske task=(ClassTaske) new ClassTaske().execute(url);
pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
pull_refresh_list.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(
PullToRefreshBase<ListView> refreshView) {
rn=10;
new ClassTaske().execute(url+rn);
}
@Override
public void onPullUpToRefresh(
PullToRefreshBase<ListView> refreshView) {
rn=5+rn;
new ClassTaske().execute(url+rn);
}
});
}
private List<Search> pulldemo(int rn) {
boolean flag = true;
try {
url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=244&pn=2&rn="+rn+"&dtype=xml";
// StringBufferInputStream in=new StringBufferInputStream(url);
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("encoding", "utf-8");
conn.connect();
if (conn.getResponseCode() == 200) {
in = conn.getInputStream();
}
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, "UTF_8");
int event = parser.getEventType();
while (flag) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<>();
break;
case XmlPullParser.START_TAG:
pname = parser.getName();
if (pname.equals("item")) {
se = new Search();
}
break;
case XmlPullParser.TEXT:
if (pname.equals("title")) {
se.setTitle(parser.getText());
} else if (pname.equals("catalog")) {
se.setCatalog(parser.getText());
} else if (pname.equals("tags")) {
se.setTags(parser.getText());
} else if (pname.equals("sub1")) {
se.setSub1(parser.getText());
} else if (pname.equals("sub2")) {
se.setSub2(parser.getText());
} else if (pname.equals("img")) {
se.setImg(parser.getText());
} else if (pname.equals("reading")) {
se.setReading(parser.getText());
} else if (pname.equals("online")) {
se.setOnline(parser.getText());
} else if (pname.equals("bytime")) {
se.setBytime(parser.getText());
}
break;
case XmlPullParser.END_TAG:
pname = parser.getName();
if (pname.equals("item")) {
list.add(se);
}
pname = "";
break;
case XmlPullParser.END_DOCUMENT:
flag = false;
break;
}
event = parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
class ClassTaske extends AsyncTask<String, Void, List<Search>> {
@Override
protected List<Search> doInBackground(String... params) {
list = pulldemo(rn);
Log.i("list", list.toString());
return list;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("提示信息");
dialog.setMessage("正在加载.......");
dialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(List<Search> result) {
super.onPostExecute(result);
dialog.cancel();
Myadapter adapter=new Myadapter(MainActivity.this,list);
pull_refresh_list.setAdapter(adapter);
adapter.notifyDataSetChanged();// 通知适配器改变
pull_refresh_list.onRefreshComplete();// 刷新完成
}
}
}
xml中PullToRefreshListView实现方式
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrDrawable="@drawable/fdff"
ptr:ptrHeaderTextColor="#f00"
ptr:ptrMode="both" />