您好,我正在尝试使用以下代码获取RSS供稿:
try {
URL url = new URL("http://investing_api.ambcrypto.com/feed/cryptolab/");
InputStream inputStream = url.openConnection().getInputStream();//error in this line
ArrayList<NewsModel> items = parseFeed(inputStream);
return items;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
它由AsyncTask和AsyncTask封装,我正在获取此日志:
07-22 23:23:03.090 11728-12314/com.mal.saul.coinmarketcap W/System.err: java.net.UnknownHostException: http://investing_api.ambcrypto.com/feed/cryptolab/
07-22 23:23:03.095 11728-13197/com.mal.saul.coinmarketcap I/FirebasePerformance: URL host is null or invalid
Unable to process an HTTP request/response due to missing or invalid values. See earlier log statements for additional information on the specific invalid/missing values.
07-22 23:23:03.110 11728-12314/com.mal.saul.coinmarketcap W/System.err: at libcore.net.http.HttpConnection$Address.<init>(HttpConnection.java:282)
07-22 23:23:03.120 11728-12314/com.mal.saul.coinmarketcap W/System.err: at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
有人有什么主意吗?我也尝试使用google news rss feed(https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us&hl=en&gl=US),并且效果很好。
方法parseFeed():
public ArrayList<NewsModel> parseFeed(InputStream inputStream) throws XmlPullParserException,
IOException {
String title = null;
String link = null;
String imageUrl = null;
long pubDate = 0;
boolean isItem = false;
ArrayList<NewsModel> items = new ArrayList<>();
try {
XmlPullParser xmlPullParser = Xml.newPullParser();
xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
xmlPullParser.setInput(inputStream, null);
xmlPullParser.nextTag();
while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {
int eventType = xmlPullParser.getEventType();
String name = xmlPullParser.getName();
if(name == null)
continue;
if(eventType == XmlPullParser.END_TAG) {
if(name.equalsIgnoreCase("item")) {
isItem = false;
}
continue;
}
if (eventType == XmlPullParser.START_TAG) {
if(name.equalsIgnoreCase("item")) {
isItem = true;
continue;
}
}
Log.d("MyXmlParser", "Parsing name ==> " + name);
String result = "";
if (xmlPullParser.next() == XmlPullParser.TEXT) {
result = xmlPullParser.getText();
xmlPullParser.nextTag();
}
if (name.equalsIgnoreCase("title")) {
title = result;
} else if (name.equalsIgnoreCase("link")) {
link = result;
} else if (name.equalsIgnoreCase("url url")) {
imageUrl = result;
} else if (name.equalsIgnoreCase("pubDate")) {
pubDate = Long.parseLong(result);
}
if (title != null && link != null && imageUrl != null && pubDate != 0) {
if(isItem) {
NewsModel newsModel = new NewsModel();
newsModel.setPostTitle(title);
newsModel.setPostUrl(link);
newsModel.setPubDate(pubDate);
items.add(newsModel);
}
title = null;
link = null;
imageUrl = null;
isItem = false;
pubDate = 0;
}
}
return items;
} finally {
inputStream.close();
}
}
答案 0 :(得分:0)
检查您的API网址,其中需要一些值。