【问题标题】:Request time failed : java.net.socketexception : Address family not supported by protocol请求时间失败:java.net.socketexception:协议不支持地址族
【发布时间】:2011-09-04 18:07:27
【问题描述】:

我已经到处搜索此异常,但找不到解决方案,我们将不胜感激。 我尝试设置断点,但它们没有被击中,该错误在 log.v 中也可见,而不是在 log.e 中。 该代码适用于最初的几次调用,例如 10-12 次,然后变慢(开始因此错误而失败),并最终每次都抛出此错误。

            _actionRunble = new Runnable() {
            public void run() {
                try{
                    ##..##
                     _imView.setImageBitmap(bmImg);
                     Drawable oldD = _imView.getBackground();
                     Drawable dd = new BitmapDrawable(bmImg);
                     _imView.setBackgroundDrawable(dd);
                     //(((BitmapDrawable)oldD).getBitmap()).recycle();
                     Thread t = new Thread(_r);
                     t.start();
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
              }
           };
           _r = new Runnable() {
            @Override
            public void run() {
                downloadFile(imageUrl);
            }
           };
           Bitmap bmImg;
           void downloadFile(String fileUrl){
            URL myFileUrl =null;          
              try {
                   myFileUrl= new URL(fileUrl);
              } catch (MalformedURLException e) {
                   e.printStackTrace();
              }
              catch (Exception e) {
                   e.printStackTrace();
              }
              try {
                   HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
                   conn.setDoInput(true);
                   conn.connect();
                   InputStream is = conn.getInputStream();

                   bmImg = BitmapFactory.decodeStream(is);
                   //this.runOnUiThread(_actionRunble);
                   _mHandler.postDelayed(_actionRunble, 2000);
                   //_mHandler.postAtFrontOfQueue(_actionRunble);
                   //_mHandler.post(_actionRunble);
              } catch (IOException e) {
                   e.printStackTrace();
              }
              catch (Exception e) {
                   e.printStackTrace();
              }
         }

activity oncreate 调用 downloadfile(...) 并在调用返回后我再次使用相同的 url 调用以获取更新的图像。我尝试将主队列上的发布消息延迟 2 秒(尽管我不希望这样)但这也不起作用:(。 请随时进一步澄清。

【问题讨论】:

  • 对于初学者,您应该断开()该 HttpURLConnection,最好在 finally 块中。使用后您并没有释放它,这听起来与运行多次的应用程序一致,您会耗尽资源。
  • 您能否将logcat 输出添加到您的问题除外?
  • @philipp:logcat 说 sntp 异常请求时间失败:java.net.socketexception:协议不支持地址系列。我想我已经解决了这个问题,如果它没有问题,我会发布答案再出故障一段时间。
  • 在问题块本身中发布了答案,因为 SO.com 允许我在 8 小时前回答,因为我的声誉得分

标签: android


【解决方案1】:

我终于解决了。 特别是为了摆脱这个异常,我开始使用单个 HttpURLConnection(重用它直到它失败,你再次创建它),但是我遇到了“BitmapFactory.decodeStream 返回 null”问题,同时执行“bmImg = BitmapFactory .decodeStream(is); “这是因为我不能在同一个连接上再次使用相同的输入流,所以我必须在再次使用它之前关闭它(is.close(),in.close())。但是出于某种原因,这种努力工作(我不知道!)。 最后,我没有从 HttpURLConnection 获取输入流(使用 conn.getInputStream() ),而是直接从 URL 获取它( myFileUrl.openStream() )。 BitmapFactory.decodeStream(is) 有时仍然可以返回 null(更好地处理这种情况 - 问我为什么 :)),在这种情况下我再次尝试下载。 这是更新的 downloadFile(...) 方法,希望它可以帮助某人:)

  void downloadFile(String fileUrl){
            URL myFileUrl =null;          
              try {
                   myFileUrl= new URL(fileUrl);
              } catch (MalformedURLException e) {
                  //print exception;
              }
              catch (Exception e) {
                  //print exception;
              }
              try {
                   //InputStream in = conn.getInputStream();--avoid this, get it from url directly instead, unless u really need to read from httpconnection because u want to configure headers or some other reason.
                   InputStream in = myFileUrl.openStream();
                   InputStream is = new BufferedInputStream(in);
                   bmImg = BitmapFactory.decodeStream(is);
                   in.close();
                   is.close();
                   if(bmImg==null)
                   {
                        downloadFile(fileUrl);
                        return;
                   }
                   //this.runOnUiThread(_actionRunble);
                   //_mHandler.postAtFrontOfQueue(_actionRunble);
                   //_mHandler.post(_actionRunble);
                   _mHandler.postDelayed(_actionRunble, 1000);
              }

             catch (IOException e) {
                  downloadFile(fileUrl);
                  //print exception;
              }
              catch (Exception e) {
                  downloadFile(fileUrl);
                  //print exception;
              }

         }

【讨论】:

    【解决方案2】:

    您的问题在于imageUrl:它可能必须以 httphttps 开头,并且您要连接的实际服务器或主机名必须是有效的 IP 地址或必须正确名称解析为 IP 地址。

    【讨论】:

    • 我的图片 url 是完全有效的,它是这样的:String imageUrl="http:// ....." ,如果 url 有问题,它永远不会工作,它工作前几秒/分钟,然后开始失败并出现此错误,不知道确切的位置或失败的原因......
    • 我发现conn.getInputStream()有问题。那是它有时会停留几分钟并记录“请求时间失败...”错误的地方,但不会抛出任何异常!
    • 您的服务器运行缓慢吗?您从中获取图像的实际位置:您可能遇到了超时,但即便如此,它也不应该抱怨地址超时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多