从远程Web资源读取文本和二进制内容时,我正在使用HttpURLConnection建立连接。

我需要实施考虑到以下问题的方法


连接到远程Web资源期间的连接超时

从远程Web资源下载内容期间。


有2个setConnectTimeout()类的设置器setReadTimeout()URLConnection用于这些目的。

当我在控制台中的计算机上运行下面这两个设置器的代码时,一切正常。

由于我的防火墙关闭了81端口,因此我将URL的规范指定为“ www.google.com:81”来模拟计算机上的连接超时问题。

如预期的10秒钟后引发异常,该异常显示在我的控制台中。

然后,我通过警告用户与远程Web资源的连接可能存在的问题来处理此异常。

但是,当我在Android平台下使用超时设置程序调用相同的方法时,在10秒后不会引发超时异常。

我搜索了所有StackOverflow并找到了在Android下使用超时时类似问题的描述。

但是给出的答案都没有指出问题的具体决定。

谁能指出确切的解决方案,如何使这些代码行所期望的setConnectTimeout()setReadTimeout()设置程序在Android下工作?

package com.downloader;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebDownloader
{
  public static String StringFileContent;
  public static boolean StringFileIsDownloaded;
  public static byte[] BinaryFileContent;
  public static boolean BinaryFileIsDownloaded;

  public static void readStringFileContent(String urlString)
  {
    StringFileContent = "";
    StringFileIsDownloaded = false;
    try
    {
      URL Url = new URL(urlString);
      HttpURLConnection connection = (HttpURLConnection)Url.openConnection();
      connection.setConnectTimeout(10000);
      connection.setReadTimeout(10000);

      connection.setRequestMethod("GET");
      connection.connect();

      InputStream inputStream = connection.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader in = new BufferedReader(inputStreamReader);

      StringBuilder response = new StringBuilder();
      String inputLine;
      while ((inputLine = in.readLine()) != null)
      {
        response.append(inputLine);
      }
      in.close();

      StringFileContent = response.toString();
      StringFileIsDownloaded = true;
    }
    catch (Exception localException)
    {
        System.out.println("Exception: " + localException.getMessage());
    }
  }

  public static void readBinaryFileContent(String urlString)
  {
    BinaryFileContent = new byte[0];
    BinaryFileIsDownloaded = false;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try
    {
      URL Url = new URL(urlString);
      HttpURLConnection connection = (HttpURLConnection)Url.openConnection();
      connection.setConnectTimeout(10000);
      connection.setReadTimeout(10000);

      connection.setRequestMethod("GET");
      connection.connect();

      InputStream inputStream = connection.getInputStream();

      byte[] chunk = new byte['?'];
      int bytesRead;
      while ((bytesRead = inputStream.read(chunk)) > 0)
      {
        outputStream.write(chunk, 0, bytesRead);
      }
      BinaryFileContent = outputStream.toByteArray();
      BinaryFileIsDownloaded = true;
    }
    catch (Exception localException)
    {
        System.out.println("Exception: " + localException.getMessage());
    }
  }

最佳答案

setConnectTimeout(int超时)
如果服务器不可靠,并且您只想等待15秒钟,然后才告诉用户“某些问题”。

setReadTimeout(int timeout)是建立连接时的超时,您在read()上被阻塞,并且如果读取阻塞的时间超过超时,您想获取异常

09-05 04:34