0

Hello i'm trying to fetch an rss feed with this code:

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();
    }

it´s wrapped by and AsyncTask and i'm getting this log:

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)

Does anybody have any idea? i also have tried with google news rss feed (https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us&hl=en&gl=US) and it works well.

Method 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();
    }
}
Saul_programa
  • 341
  • 1
  • 4
  • 13

1 Answers1

0

Check your API url it requires some values in it.

Sachin Soma
  • 3,432
  • 2
  • 10
  • 18