2011-09-04 157 views
1

我已經搜索了這個例外無處不在,但無法找到解決方案&任何幫助,將不勝感激。 我已經試過把斷點,但他們不被打,錯誤也可以在log.v中看到,而不是在log.e. 該代碼適用於第一次調用10-12次,然後變慢(開始失敗,出現此錯誤),並最終每次都會拋出此錯誤。請求時間失敗:java.net.socketexception:協議不支持的地址族

  _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秒(雖然我不希望那樣),但那並不工作太。(。 請隨時進一步澄清。

+0

對於初學者你應該斷開()HttpURLConnection,最好是在finally塊中。使用後你沒有釋放它,這聽起來是c與運行多次的應用程序保持一致,則會耗盡資源。 – fvu

+0

做到了這一點,爲此。 –

+0

你可以添加'logcat'輸出與你的問題的例外嗎? –

回答

2

嗯,我解決了它最後。 要特別是擺脫這個異常,我開始使用單個HttpURLConnection(重用它,直到它失敗,你再次創建它),但與我鐳n變成「BitmapFactory.decodeStream返回null」的問題,同時做「bmImg = BitmapFactory.decodeStream(is); 「這是因爲我再也不能在相同的連接上使用相同的輸入流,所以我必須在再次使用它之前關閉它(is.close(),in.close())。但是這個功能適用於某些原因(我不知道!) 終於不是從HttpURLConnection獲取inputstream(使用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; 
       } 

     } 
0

您的問題是與imageUrl:它可能必須開始與HTTPHTTPS,和實際的服務器或要連接必須是一個有效的IP地址或必須正確名稱解析爲IP地址的主機名。

+0

我的圖片網址完全有效,它的內容如下所示:String imageUrl =「http://.....「,同樣如果url有問題,它也曾經有過工作,它在最初的幾秒/分鐘內工作,然後開始失敗,出現這個錯誤,不知道究竟在哪裏或爲什麼失敗... –

+0

我發現conn.getInputStream()有問題。多數民衆贊成在有些地方呆了幾分鐘,並記錄「請求時間失敗...」的錯誤,但沒有拋出任何異常! –

+0

您的服務器運行緩慢嗎?您從中獲取圖片的實際位置:您可能有超時,但即使如此,也不應該抱怨地址超時。 – Femi

相關問題