【问题标题】:Read from URL java从 URL 读取 java
【发布时间】:2011-06-07 22:34:13
【问题描述】:

我正在尝试在 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();
    }

【问题讨论】:

  • 介意发布一些相关代码吗?

标签: java url timeout


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

      URLConnection conn = url.openConnection();
      // setting timeouts
      conn.setConnectTimeout(connectTimeoutinMilliseconds);
      conn.setReadTimeout(readTimeoutinMilliseconds);
      InputStream in = conn.getInputStream();
      

    参考:URLConnection#setReadTimeoutURLConnection#setConnectTimeout

    • 或者,您应该将sun.net.client.defaultConnectTimeoutsun.net.client.defaultReadTimeout system property 设置为合理的值。

    【讨论】:

      【解决方案3】:

      如果您使用 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();
      }
      

      【讨论】:

      • 我猜你使用URL 然后调用openConnection() 它返回一个URLConnection
      • 好的,我刚刚添加了修改后的代码——我相信您可以通过BufferedReader 发现这两条新行和行中的更改
      【解决方案4】:

      您应该在 AndroidMenifest.xml

      中添加互联网权限
      <uses-permission android:name="android.permission.INTERNET" />
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-19
        • 2013-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        相关资源
        最近更新 更多