请求时间失败 : java.net.socketexception : 协议不支持的地址族[英] Request time failed : java.net.socketexception : Address family not supported by protocol

本文是小编为大家收集整理的关于请求时间失败 : java.net.socketexception : 协议不支持的地址族的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我到处搜索这个异常,但找不到解决方案&任何帮助,将不胜感激.我试过放置断点,但它们没有被击中,错误也在 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(重用它直到它失败,你再次创建它),但是我遇到了"BitmapFactory.decodeStream返回空"问题,同时做"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;
              }

         }

其他推荐答案

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

本文地址:https://www.itbaoku.cn/post/102675.html

问题描述

i have searched for this exception everywhere but could not find a resolution & any help would be appreciated. i have tried putting break points, but they do not get hit, the error is also visible in log.v and not in log.e. The code works for first few calls say for 10-12 times, then gets slower(starts failing with this error), and eventually throws this error every-time.

            _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 calls downloadfile(...) and after return of the call i again call with the same url to get the updated image. I tried delaying the posting message on main queue by 2 secs(although i dont want that) but that doesnt works too :( . Please feel free for further clarification.

推荐答案

well i resolved it finally. To get rid of this exception in particular, i started using a single HttpURLConnection(reuse it until it fails, where u create it again), BUT with that i ran into "BitmapFactory.decodeStream returning null" problem, while doing "bmImg = BitmapFactory.decodeStream(is); " This was due to the fact i cannot use the same inputstream over the same connection again, so i had to close it before using it again(is.close(), in.close()). but that dint work for some reason(i dont know!). finally instead of getting inputstream from HttpURLConnection (using conn.getInputStream() ) i directly get it from URL ( myFileUrl.openStream() ). BitmapFactory.decodeStream(is) still can return null sometimes(better to handle that case-ask me why :) ), in which case i try the download again. here is the updated downloadFile(...) method, Hope it helps someone :)

  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;
              }

         }

其他推荐答案

Your problem is with imageUrl: it probably has to start with http or https, and the actual server or hostname you're connecting to must be either a valid IP address or must name-resolve correctly to an IP address.