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

查看:239
本文介绍了请求时失败:产生java.net.SocketException:地址族不支持的协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找这个异常比比皆是,但找不到一个分辨率和放大器;任何帮助,将AP preciated。 我试图把破发点,但他们没有被打到,错误也是log.v可见的,而不是在log.e. 在code适用于前几个电话说,10至12次,然后变得更慢(启动与此错误失败的),并最终引发此错误每次时间。

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

活动的OnCreate调用downloadfile(...),并回电话我再次用相同的URL调用来获取更新的图像数据。我试图通过2秒延迟对主队列张贴消息(尽管我不希望出现这种情况),但是,这并不工程太:(。 请随时进一步澄清。

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.

推荐答案

嗯,我解决了它最后。 要摆脱这种异常特别的,我开始使用一个单一的HttpURLConnection(直到失败重新使用它,其中u重新创建),但我遇到了BitmapFactory.de codeStream返回null的问题,而这样做bmImg = BitmapFactory.de codeStream(是); 这是由于这一事实,我不能再次使用相同的InputStream通过相同的连接,所以我不得不再次使用它(is.close(),in.close()),然后将其关闭。但由于某些原因,力工作(我不知道!)。 终于而不是从HttpURLConnection的获得的InputStream(使用conn.getInputStream()),我直接从URL得到它(myFileUrl.openStream())。 BitmapFactory.de codeStream(是)仍然可以返回NULL有时(更好地处理这种情况下,问我为什么:)),在这种情况下,我重新尝试下载。 这里是更新downloadFile(...)方法,希望它可以帮助别人:)

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

         }

这篇关于请求时失败:产生java.net.SocketException:地址族不支持的协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆