找不到java.net.malformedurlexception协议

时间:2012-09-10 18:06:03

标签: java http protocols malformedurlexception

我正在使用以下代码,它一直给我malformedurlexception错误。有趣的是我从另一个相同代码正常工作的项目中复制了它。我想知道是否有人能看出问题所在。给出错误的代码部分在OpenHttpConnection函数中,并在开始时显示:

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;     

1 个答案:

答案 0 :(得分:-5)

更改为:

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