从 URL 读取 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6272405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read from URL java
提问by Gandalf StormCrow
I'm trying to read URL in java, and it works as long as the URL is loading in browser.
我正在尝试用 Java 读取 URL,只要 URL 在浏览器中加载,它就可以工作。
But if it is just cylcing in the browser and not loading that page when I'm trying to open it in my browser, my java app just hangs, it will probably wait forever given enough time. How do I set timeout on that or something, if its loading for more than 20 seconds that I stop my application?
但是,如果它只是在浏览器中循环并且在我尝试在浏览器中打开它时没有加载该页面,那么我的 Java 应用程序就会挂起,如果有足够的时间,它可能会永远等待。如果我停止应用程序的加载时间超过 20 秒,我该如何设置超时?
I'm using URL
我正在使用网址
Here is a relevant part of the code :
这是代码的相关部分:
URL url = null;
String inputLine;
try {
url = new URL(surl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
回答by knurdy
I don't know how u are using the URL class. It would have been better if post a snippet. But here is a way that works for me. See if it helps in your case:
我不知道你是如何使用 URL 类的。如果贴个片段就更好了。但这是一种对我有用的方法。看看它是否对你的情况有帮助:
URL url = new URL(urlPath);
URLConnection con = url.openConnection();
con.setConnectTimeout(connectTimeout);
con.setReadTimeout(readTimeout);
InputStream in = con.getInputStream();
回答by Piyush Mattoo
The URL#openStreammethod is actually just a shortcut for openConnection().getInputStream()
. Here is the code from the URL class:
该URL#OpenStream的方法实际上只是一个快捷方式openConnection().getInputStream()
。这是来自 URL 类的代码:
public final InputStream openStream() throws java.io.IOException {
return openConnection().getInputStream();
}
You can adjust settings in the client code as follows:
URLConnection conn = url.openConnection(); // setting timeouts conn.setConnectTimeout(connectTimeoutinMilliseconds); conn.setReadTimeout(readTimeoutinMilliseconds); InputStream in = conn.getInputStream();
您可以在客户端代码中调整设置,如下所示:
URLConnection conn = url.openConnection(); // setting timeouts conn.setConnectTimeout(connectTimeoutinMilliseconds); conn.setReadTimeout(readTimeoutinMilliseconds); InputStream in = conn.getInputStream();
Reference: URLConnection#setReadTimeout, URLConnection#setConnectTimeout
参考:URLConnection#setReadTimeout, URLConnection#setConnectTimeout
- Alternatively, you should set the
sun.net.client.defaultConnectTimeout
andsun.net.client.defaultReadTimeout
system propertyto a reasonable value.
- 或者,您应该将
sun.net.client.defaultConnectTimeout
和sun.net.client.defaultReadTimeout
系统属性设置为一个合理的值。
回答by Liv
If you are using a URLConnection
(or HttpURLConnection
) to "read from a url" you have a setReadTimeout()
method which allows you to control that.
如果您使用 a URLConnection
(or HttpURLConnection
) 来“从 url 读取”,则您可以使用一种setReadTimeout()
方法来控制它。
Edited after you posted the code:
发布代码后编辑:
URL url = null;
String inputLine;
try {
url = new URL(surl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
BufferedReader in;
try {
URLConnection con = url.openConnection();
con.setReadTimeout( 1000 ); //1 second
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
回答by Muhammad Hassan
You should add internet permission in AndroidMenifest.xml
您应该在AndroidMenifest.xml 中添加互联网权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />
and add it in the mainfunction:
并将其添加到主函数中:
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}