HttpURLConnection请求网络数据
private InputStream httpUtil(String uri) {
try {
URL url=new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
in=conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
//pull解析
private List<Book> pull(InputStream s) {
pull=Xml.newPullParser();
try {
pull.setInput(s,"utf-8");
eve=pull.getEventType();
while(flag){
switch (eve) {
case XmlPullParser.START_DOCUMENT:
list=new ArrayList<Book>();
break;
case XmlPullParser.START_TAG:
st=pull.getName();
if(st.equals("item")){
b = new Book();
}
break;
case XmlPullParser.TEXT:
if(st.equals("id")){
b.setId(pull.getText());
}else if(st.equals("catalog")){
b.setName(pull.getText());
}
break;
case XmlPullParser.END_TAG:
st=pull.getName();
if(st.equals("item")){
list.add(b);
}
st="";
break;
case XmlPullParser.END_DOCUMENT:
flag=false;
break;
}
eve=pull.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}