public class GethttpURLconnection {
private static ArrayList<Bean> list;
private static Bean bean;
public static ArrayList<Bean> getInput() {
list = new ArrayList<Bean>();
URL url;
try {
url = new URL(
"http://v.juhe.cn/weixin/query?pno=&ps=&dtype=xml&key=664dfc66cc911fcdef324ea6cd34e30f");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int response = connection.getResponseCode();
if (response == 200) {
InputStream inputStream = connection.getInputStream();
// 得到pull解析器
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "utf-8");
// 解析的类型
int eventType = parser.getEventType();
String str = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
str = parser.getName();
if (str.equals("item")) {
bean = new Bean();
}
break;
case XmlPullParser.TEXT:
if (str.equals("firstImg")) {
bean.fristImg = parser.getText().trim();
} else if (str.equals("id")) {
bean.id = parser.getText().trim();
} else if (str.equals("source")) {
bean.source = parser.getText().trim();
} else if (str.equals("title")) {
bean.title = parser.getText().trim();
} else if (str.equals("url")) {
bean.url = parser.getText().trim();
}
break;
case XmlPullParser.END_TAG:
str = parser.getName();
if (str.equals("item")) {
list.add(bean);
bean = null;
}
str = "";
break;
case XmlPullParser.END_DOCUMENT:
break;
}
eventType = parser.next();
}
return list;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static ArrayList<Bean> list;
private static Bean bean;
public static ArrayList<Bean> getInput() {
list = new ArrayList<Bean>();
URL url;
try {
url = new URL(
"http://v.juhe.cn/weixin/query?pno=&ps=&dtype=xml&key=664dfc66cc911fcdef324ea6cd34e30f");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int response = connection.getResponseCode();
if (response == 200) {
InputStream inputStream = connection.getInputStream();
// 得到pull解析器
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "utf-8");
// 解析的类型
int eventType = parser.getEventType();
String str = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
str = parser.getName();
if (str.equals("item")) {
bean = new Bean();
}
break;
case XmlPullParser.TEXT:
if (str.equals("firstImg")) {
bean.fristImg = parser.getText().trim();
} else if (str.equals("id")) {
bean.id = parser.getText().trim();
} else if (str.equals("source")) {
bean.source = parser.getText().trim();
} else if (str.equals("title")) {
bean.title = parser.getText().trim();
} else if (str.equals("url")) {
bean.url = parser.getText().trim();
}
break;
case XmlPullParser.END_TAG:
str = parser.getName();
if (str.equals("item")) {
list.add(bean);
bean = null;
}
str = "";
break;
case XmlPullParser.END_DOCUMENT:
break;
}
eventType = parser.next();
}
return list;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}