2012-09-10 141 views
3

我正在使用下面的代碼,它不斷給我malformaurlexception錯誤。有趣的部分是我從另一個相同的代碼正常工作的項目中複製它。我想知道是否有人可以看到問題可能是什麼。哪個是給錯誤代碼的部分是OpenHttpConnection功能,在開始的地方說:java.net.malformedurlexception協議未找到

URL url = new URL(urlString); 
URLConnection conn = url.openConnection(); 

我感謝所有幫助我能。

Bitmap bitmapOrg = null; 
bitmapOrg = DownloadImage("http://www.bagherpour.com/apache_pb.gif"); 

public Bitmap DownloadImage(String txt) 
{  

    Bitmap bitmap = null; 
    InputStream in = null; 
    try {   
     in = OpenHttpConnection(txt); 
     bitmap = BitmapFactory.decodeStream(in); 

     in.close(); 
     return bitmap;  
    } catch (IOException e1) { 
     e1.printStackTrace(); 
     Log.d("bitMap",e1.toString()); 
     return null; 
    }     
} 

public InputStream OpenHttpConnection(String urlString) 
    throws IOException 
     { 
      InputStream in = null; 
      int response = -1; 

      URL url = new URL(urlString); 
      URLConnection conn = url.openConnection(); 

      if (!(conn instanceof HttpURLConnection))      
       throw new IOException("Not an HTTP connection"); 

      try{ 
       HttpURLConnection httpConn = (HttpURLConnection) conn; 
       httpConn.setAllowUserInteraction(false); 
       httpConn.setInstanceFollowRedirects(true); 
       httpConn.setRequestMethod("GET"); 
       httpConn.connect(); 

       response = httpConn.getResponseCode();     
       if (response == HttpURLConnection.HTTP_OK) { 
        in = httpConn.getInputStream();         
       }      
      } 
      catch (Exception ex) 
      { 
       throw new IOException("Error connecting");    
      } 
      return in;  
+2

你得到的URL錯誤。很明顯,它不是以http://開頭的。 – EJP

+0

但我有硬編碼的http://到urlString。怎樣才能解決它? – Farshid

+0

你在MalformedURLException上得到了什麼? –

回答

-4

更改爲:

URL rul = new URL("httpwww.bagherpour.com/apache_pb.gif") 
+0

改變什麼?另外,請解釋原因。 – Mark

+1

這是垃圾。它只會給你另一個格式不正確的URL。 –

相關問題