2017-09-25 39 views
0

从远程Web资源中读取文本和二进制内容时,我使用HttpURLConnection建立连接。如何使HttpURLConnection setConnectTimeout在Android下工作?

我需要实现的方法,从远程网络资源内容下载过程中连接到远程网络资源 和

  • 过程中考虑了与

    • 连接超时可能出现的问题。

    有setter方法2和setConnectTimeout()被用于这些目的的URLConnection类的setReadTimeout()

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

    我使用URL规范为“www.google.com:81”来模拟计算机上的连接超时问题,因为81端口被我的防火墙关闭。

    按预期在10秒后引发异常并显示在我的控制台中。

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

    但是当我在Android平台下调用与timeout setters相同的方法时,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());   
        } 
        } 
    
  • +0

    你得到任何*得到一个异常*例外?请注意粗体。它没有帮助。 – EJP

    +0

    是的。大约40秒后出现异常(例外:无法解析主机“www.google.com”:没有与主机名关联的地址) – Inessa

    +0

    您是否尝试过使用Chrome浏览器在Android设备上加载相同的网址? – Dryr

    回答

    1

    setConnectTimeout(INT超时) 如果有不可靠的服务器,并且希望你告诉用户说:“有些事情不对”之前只等待15秒。

    setReadTimeout(INT超时)是当你有一个连接,你阻塞的read()的超时,你想,如果超过超时更读取块

    相关问题