Java 如何使HttpURLConnection setConnectTimeout在Android下工作?

Java 如何使HttpURLConnection setConnectTimeout在Android下工作?,java,android,httpurlconnection,Java,Android,Httpurlconnection,在从远程web资源读取文本和二进制内容时,我使用HttpURLConnection建立连接 我需要实施一种考虑到可能出现的问题的方法 连接到远程web资源期间的连接超时 和 从远程web资源下载内容期间。 URLConnection类有两个setter setConnectTimeout和setReadTimeout用于这些目的 当我在控制台中的计算机上运行下面给出的代码并实现这两个setter时,一切正常 我使用URL的规范www.google.com:81来模拟计算机上的连接超时问题,因为8

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

我需要实施一种考虑到可能出现的问题的方法

连接到远程web资源期间的连接超时 和 从远程web资源下载内容期间。 URLConnection类有两个setter setConnectTimeout和setReadTimeout用于这些目的

当我在控制台中的计算机上运行下面给出的代码并实现这两个setter时,一切正常

我使用URL的规范www.google.com:81来模拟计算机上的连接超时问题,因为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());        
    }
  }

SetConnectTimeOutit超时 如果存在不可靠的服务器,并且您只想等待15秒,然后告诉用户出了问题


SetReadTimeOutit timeout是当您有连接时的超时,您在读取时被阻止,并且如果读取阻塞超过超时,您希望获得异常

您是否获得任何异常?注意,请去掉黑体字。这没用,是的。大约40秒后我遇到异常异常异常:无法解析主机www.google.com:没有与主机名相关的地址您是否尝试在android设备上使用chrome navigator加载相同的url?@Inessa:考虑到您上面的评论,您的DNS似乎无法解析主机名,这意味着它无法获取与域名关联的IP地址。No。android设备上的chrome navigator无法提供测试url。