XUtils
//HttpUtils实例化对象
HttpUtils http = new HttpUtils();
/*
*发送请求send(HttpMethod method, String url, RequestCallBack<T> callBack)
* method请求方式
* url请求地址
*RequestCallBack <String>请求完后的回调监听String是请求完后你想让他返回什么类型的
*/
http.send(HttpRequest.HttpMethod.GET, urlPath,new RequestCallBack<String>() {
@Override
public void onLoading(long total, long current, boolean isUploading) {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String s = responseInfo.result;
// System.out.println(s);
try {
//判断,如果有数据先清空
if (STATE_3 == STATE_1) {
news .clear();
}
News newss=null;
XmlPullParser parser=Xml.newPullParser();
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(s.getBytes());
parser.setInput(byteArrayInputStream, "utf-8");
int eventType=parser.getEventType();
while (eventType!=XmlPullParser.END_DOCUMENT) {
String name=parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (name.equals("news")) {
newss=new News();
}else if (name.equals("title")) {
String title=parser.nextText();
newss.setTitle(title);
}
else if (name.equals("body")) {
String body=parser.nextText();
newss.setBody(body);
}
else if (name.equals("commentCount")) {
String commentCount=parser.nextText();
newss.setCommentCount(commentCount);
}
else if (name.equals("author")) {
String author=parser.nextText();
newss.setAuthor(author);
}else if (name.equals("pubDate")) {
String pubDate=parser.nextText();
newss.setPubDate(pubDate);
}
break;
case XmlPullParser.END_TAG:
if (name.equals("news")) {
news.add(newss);
//更新适配器
if (adapter == null) {
adapter = new MyAdapter(news,getActivity());
lv.setAdapter(adapter);
} else {
adapter .notifyDataSetChanged();
}
load();
}
break;
default:
break;
}
eventType=parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onStart() {
}
@Override
public void onFailure(HttpException error, String msg) {
}
});