Request time failed : java.net.socketexception : Address family not supported by protocol

后端 未结 2 1505
孤独总比滥情好
孤独总比滥情好 2021-01-07 11:08

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

相关标签:
2条回答
  • 2021-01-07 11:50

    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.

    0 讨论(0)
  • 2021-01-07 11:55

    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;
                  }
    
             }
    
    0 讨论(0)
提交回复
热议问题