从 URL java 读取

发布于 2024-11-14 12:04:43 字数 765 浏览 0 评论 0原文

我正在尝试用 java 读取 URL,只要 URL 在浏览器中加载,它就可以工作。

但是,如果它只是在浏览器中循环,并且当我尝试在浏览器中打开它时不加载该页面,我的 java 应用程序就会挂起,如果有足够的时间,它可能会永远等待。如果加载时间超过 20 秒而导致我停止应用程序,我该如何设置超时或其他内容?

我正在使用 URL

这是代码的相关部分:

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

I'm trying to read URL in java, and it works as long as the URL is loading in browser.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老娘不死你永远是小三 2024-11-21 12:04:43

我不知道你如何使用 URL 类。如果贴个片段就更好了。但这是一种对我有用的方法。看看它对您的情况是否有帮助:

    URL url = new URL(urlPath);
    URLConnection con = url.openConnection();
    con.setConnectTimeout(connectTimeout);
    con.setReadTimeout(readTimeout);
    InputStream in = con.getInputStream();

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 = new URL(urlPath);
    URLConnection con = url.openConnection();
    con.setConnectTimeout(connectTimeout);
    con.setReadTimeout(readTimeout);
    InputStream in = con.getInputStream();
瞎闹 2024-11-21 12:04:43

URL#openStream 方法实际上只是 openConnection().getInputStream() 的快捷方式。以下是 URL 类的代码:

public final InputStream openStream() throws java.io.IOException {
  return openConnection().getInputStream();
}
  • 您可以按如下方式调整客户端代码中的设置:

    URLConnection conn = url.openConnection();
    // 设置超时时间
    conn.setConnectTimeout(connectTimeoutinMilliseconds);
    conn.setReadTimeout(readTimeoutinMilliseconds);
    输入流= conn.getInputStream();
    

参考:URLConnection#setReadTimeout, URLConnection#setConnectTimeout

  • 或者,您应该设置 sun.net.client.defaultConnectTimeoutsun.net.client.defaultReadTimeout 系统属性设置为合理的值。

The URL#openStream method is actually just a shortcut for openConnection().getInputStream(). Here is the code from the URL class:

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

Reference: URLConnection#setReadTimeout, URLConnection#setConnectTimeout

  • Alternatively, you should set the sun.net.client.defaultConnectTimeout and sun.net.client.defaultReadTimeout system property to a reasonable value.
风追烟花雨 2024-11-21 12:04:43

如果您使用 URLConnection (或 HttpURLConnection)“从 url 读取”,您可以使用 setReadTimeout() 方法来控制那。

发布代码后进行编辑:

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

If you are using a URLConnection (or HttpURLConnection) to "read from a url" you have a setReadTimeout() method which allows you to control that.

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();
}
第几種人 2024-11-21 12:04:43

您应该在 AndroidMenifest.xml 中添加互联网权限

>

并将其添加到 ma​​in 函数中:

if (android.os.Build.VERSION.SDK_INT > 9)
        {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

You should add internet permission in AndroidMenifest.xml

<uses-permission android:name="android.permission.INTERNET" />

and add it in the main function:

if (android.os.Build.VERSION.SDK_INT > 9)
        {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文