package com.example.wangjiaxin20160516;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import com.example.wangjiaxin20160516.xlistviews.XListView;
import com.example.wangjiaxin20160516.xlistviews.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.thoughtworks.xstream.XStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements IXListViewListener{
private String qwe;
StringBuffer sb = new StringBuffer();
private XListView listview;
private List<Data3> item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (XListView) findViewById(R.id.listview);
listview.setPullLoadEnable(true);//设置下拉刷新
listview.setPullRefreshEnable(true);//设置监听事件,重写两个方法
listview.setXListViewListener(this);//设置上拉刷新
new Thread() {
public void run() {
try {
final URL url = new URL(
"http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=0&rn=20&dtype=xml");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStreamReader inputStreamReader = new InputStreamReader(
connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
while ((qwe = bufferedReader.readLine()) != null) {
sb.append(qwe);
}
}
} catch (Exception e) {
e.printStackTrace();
}
String str = sb.toString();
XStream stream = new XStream();
stream.processAnnotations(Data.class);
Data data = (Data) stream.fromXML(str);
item = data.getResult().getData().getItem();
runOnUiThread(new Runnable() {
public void run() {
listview.setAdapter(new MyAdapter());
}
});
}
}.start();
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return item.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
arg1 = arg1.inflate(MainActivity.this, R.layout.item, null);
ImageView im = (ImageView)arg1.findViewById(R.id.im);
TextView text = (TextView) arg1.findViewById(R.id.text);
TextView text1 = (TextView)arg1.findViewById(R.id.text1);
TextView text2 = (TextView)arg1.findViewById(R.id.text2);
BitmapUtils bitmapUtils = new BitmapUtils(MainActivity.this);
bitmapUtils.display(im,item.get(arg0).getImg());
text.setText(item.get(arg0).getTitle());
text1.setText(item.get(arg0).getCatalog());
text2.setText(item.get(arg0).getSub1());
return arg1;
}
}
@Override
public void onRefresh() {
new Thread() {
public void run() {
try {
final URL url = new URL(
"http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=0&rn=20&dtype=xml");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStreamReader inputStreamReader = new InputStreamReader(
connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
while ((qwe = bufferedReader.readLine()) != null) {
sb.append(qwe);
}
}
} catch (Exception e) {
e.printStackTrace();
}
String str = sb.toString();
XStream stream = new XStream();
stream.processAnnotations(Data.class);
Data data = (Data) stream.fromXML(str);
item = data.getResult().getData().getItem();
runOnUiThread(new Runnable() {
public void run() {
listview.setAdapter(new MyAdapter());
}
});
}
}.start();
}
@Override
public void onLoadMore() {
new Thread() {
public void run() {
try {
final URL url = new URL(
"http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=0&rn=20&dtype=xml");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStreamReader inputStreamReader = new InputStreamReader(
connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
while ((qwe = bufferedReader.readLine()) != null) {
sb.append(qwe);
}
}
} catch (Exception e) {
e.printStackTrace();
}
String str = sb.toString();
XStream stream = new XStream();
stream.processAnnotations(Data.class);
Data data = (Data) stream.fromXML(str);
item = data.getResult().getData().getItem();
runOnUiThread(new Runnable() {
public void run() {
listview.setAdapter(new MyAdapter());
}
});
}
}.start();
}
}
转载于:https://www.cnblogs.com/3674-it/p/5551260.html