Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 URL连接中的“java.net.UnknownHostException”错误?_Java_Url - Fatal编程技术网

如何消除java URL连接中的“java.net.UnknownHostException”错误?

如何消除java URL连接中的“java.net.UnknownHostException”错误?,java,url,Java,Url,我想在每个连接实例之后关闭URL连接 我有一个类似- 这段代码是 String URLfile = "https://www.google.com/company_name";// So to this URL I am //adding 100 Identities. //There are over 50000 identities and I am passing URLs //by appending identities with a batch size of 100. t

我想在每个连接实例之后关闭URL连接

我有一个类似-

这段代码是

String URLfile = "https://www.google.com/company_name";// So to this URL I am 
//adding 100 Identities. 
//There are over 50000 identities and I am passing URLs 
//by appending identities with a batch size of 100. 

try{

// the length of the business Id is '100'
for(int i=0;i<businessId.length;i++){
            URLdata+=","+businessId[i];
        }
        System.out.println("the URL :"+URLdata);

        Step 1> Pass the URL
        URL url = new URL(URLdata);

        URLConnection conn = url.openConnection();

        // Step 2> to get the information from the website as an 'input stream'
        // Steps to get the data as an 'input stream' for the returned data 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());

        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();

        // Step 3> Output the 'input  stream' onto an user defined 'XML' file
        // that's the default xform; use a stylesheet to get a real one
        // output the xml(input stream) information on to an XML file in the field specified in the program. 
        File myOutput = new File(XMLOutput);
        xform.transform(new DOMSource(doc), new StreamResult(myOutput));

        // Step 4> to parse the XML file to get the fields required for updating the database
        // Here we are parsing the XML file to get the information as specified
        // parsing the xml file for the information necessary 
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document docum = docBuilder.parse (new File(XMLOutput));

        // Step 4a) 
        // Read the XML file using the XPath for parsing the XML information
        // Creating an 'instance' of the XPath method
        XPath xPath =  XPathFactory.newInstance().newXPath();


}

 catch(Exception e){
 System.out.println("There is an error :"+e);
 }

我得到一个java.net.UnknownHostException。是否有可能我在每个实例之后关闭连接,因此它抛出错误?我正在尝试conn.close,但没有这样的功能。非常感谢您的帮助。

我不确定开始时的for循环为您做了什么,但未知的后异常可能是由URL=new urldata抛出的;因为它不知道如何解释URLdata中的内容。检查并确保println的输出始终是构造函数喜欢的格式。

通过使用

HttpURLConnection conn = (HttpURLConnection) url
        .openConnection();
而不是

URLConnection conn = url.openConnection();
它解决了超时和连接过多的问题


感谢所有建议和参考。

请确保在清单文件中包含此权限:


UnknownHostException是不言自明的-JVM无法解析您使用的主机名1从何处抛出UnknownHostException?2什么是URL?如果您试图从浏览器中使用它,它会解决问题吗?请检查堆栈跟踪和url。openConnection@Pedantic编辑问题以包含更多详细信息。@fge如何解决问题,因为我在浏览器中输入信息时使用相同的URL获取信息。编辑问题以包含更多详细信息。至于URL构造的情况,我在浏览器中使用相同的URL构造,它返回信息。谢谢你的邀请。有什么建议吗?我对这种情况下URLfile和URLdata的模糊性感到困惑。首先,您将不断地向URLFile追加一个数字,以便在结束时字符串如下所示https://www.google.com/company_name12345678910...100,但您甚至没有在这个特定代码中使用该字符串。那么,变量URLdata中实际存储了什么呢?我已经更正了它,并将URLfile更改为URLdata。感谢您指出错误。不,仍然不是,错误发生在我刚刚在StackOverflow上发布问题时,而不是在程序中,它运行并给出正确的输出一段时间,但之后它抛出了错误。我认为您确实试图用存储在businessID数组中的名称替换company_名称,在这种情况下,您还应该更改字符串结构以删除公司名称。不管怎样,当UnknownHostException无法解析指定URL的IP地址时,将引发UnknownHostException。如果您正在生成不在DNS中转换的随机名称,那么当您尝试创建对象时,将抛出该名称。