【问题标题】:java.net.malformedurlexception protocol not found [duplicate]java.net.malformedurlexception 协议未找到 [重复]
【发布时间】:2012-09-03 15:24:07
【问题描述】:

我正在使用以下代码,但它不断给我 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;     

【问题讨论】:

  • 您的网址有误。显然它根本不是以 http:// 开头的。
  • 但我将 http:// 硬编码到 urlString 中。怎样才能正确?
  • 您在哪一行得到 MalformedURLException?
  • 我把代码改成了 URL url = new URL("http","www.bagherpour.com","/apache_pb.gif");然后 MalformedURLException 消失了,但我仍然无法连接。它说 W/System.err(968): java.io.IOException: Error connected。当我执行 httpConn.connect(); 时会发生这种情况

标签: java http protocols malformedurlexception


【解决方案1】:

改为:

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

【讨论】:

  • 具体改什么?另外,请解释原因。
  • 这是垃圾。它只会给你另一个格式错误的 URL。
猜你喜欢
  • 2018-11-08
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
相关资源
最近更新 更多