Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取java.net.UnknownHostException:_Java_Android_Host_Urlconnection_Unknown Host - Fatal编程技术网

获取java.net.UnknownHostException:

获取java.net.UnknownHostException:,java,android,host,urlconnection,unknown-host,Java,Android,Host,Urlconnection,Unknown Host,您好,我正在尝试获取具有以下代码的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 = pars

您好,我正在尝试获取具有以下代码的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();
    }
试试看{
URL=新URL(“http://investing_api.ambcrypto.com/feed/cryptolab/");
InputStream InputStream=url.openConnection().getInputStream();//此行出错
ArrayList items=parseFeed(inputStream);
退货项目;
}捕获(IOE异常){
e、 printStackTrace();
}catch(XMLPullParseRexE){
e、 printStackTrace();
}
它由和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)
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主机为空或无效
由于缺少或无效值,无法处理HTTP请求/响应。有关特定无效/缺失值的更多信息,请参阅前面的日志语句。
07-22 23:23:03.110 11728-12314/com.mal.saul.coinmarketcap W/System.err:at libcore.net.http.HttpConnection$地址。(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)
有人知道吗?我也尝试过谷歌新闻rss提要(),效果很好

方法parseFeed():

public ArrayList parseFeed(InputStream InputStream)抛出XmlPullParserException,
IOException{
字符串标题=null;
字符串链接=null;
字符串imageUrl=null;
长pubDate=0;
布尔isItem=false;
ArrayList items=新建ArrayList();
试一试{
XmlPullParser XmlPullParser=Xml.newPullParser();
setFeature(xmlPullParser.FEATURE_进程_名称空间,false);
setInput(inputStream,null);
xmlPullParser.nextTag();
while(xmlPullParser.next()!=xmlPullParser.END_文档){
int eventType=xmlPullParser.getEventType();
String name=xmlPullParser.getName();
if(name==null)
继续;
if(eventType==XmlPullParser.END_标记){
if(名称、相等信号情况(“项目”)){
isItem=假;
}
继续;
}
if(eventType==XmlPullParser.START_标记){
if(名称、相等信号情况(“项目”)){
isItem=真;
继续;
}
}
Log.d(“MyXmlParser”,“解析名称==>”+名称);
字符串结果=”;
if(xmlPullParser.next()==xmlPullParser.TEXT){
结果=xmlPullParser.getText();
xmlPullParser.nextTag();
}
if(名称、同等信号案例(“标题”)){
标题=结果;
}else if(name.equalsIgnoreCase(“链接”)){
链接=结果;
}else if(name.equalsIgnoreCase(“url”)){
imageUrl=结果;
}else if(name.equalsIgnoreCase(“pubDate”)){
pubDate=Long.parseLong(结果);
}
if(title!=null&&link!=null&&imageUrl!=null&&pubDate!=0){
如果(isItem){
NewsModel NewsModel=新的NewsModel();
newsModel.setPostTitle(title);
newsModel.setpostrl(链接);
newsModel.setPubDate(pubDate);
items.add(新闻模型);
}
title=null;
link=null;
imageUrl=null;
isItem=假;
pubDate=0;
}
}
退货项目;
}最后{
inputStream.close();
}
}

检查API url,它需要一些值。

删除
url
http://investing_api.ambcrypto.com/feed/cryptolab
你的第二行代码非常好,但是你能分享你的parseFeed方法来理解实际问题吗?@LwinMyoAung它没有修复it@Devratna好啊我刚刚添加了它。你能在浏览器中打开这个url吗?你能告诉我哪些值吗?把链接发送到你得到这个链接的地方吗?没有文档
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();
    }
}