简体   繁体   English

Android-未处理的java.net.MalformedURLException

[英]Android - Unhandled java.net.MalformedURLException

I'm getting a MalformedURLException some code in my Android Studio project. 我的Android Studio项目中出现了MalformedURLException一些代码。 My aim is to get and display an image from a web page, and the URL seems to be fine, but it's giving me this error. 我的目的是从网页上获取并显示图像,URL看起来不错,但这给了我这个错误。

I have already put the code in a try , catch , but that is still giving me the error. 我已经将代码放在trycatch ,但这仍然给我错误。 Here is the code to grab the image and display it: 这是获取图像并显示它的代码:

    try
    {
        url = new URL(items.get(position).getLink());
        bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    }
    catch (IOException e)
    {
        throw new RuntimeException("Url Exception" + e);
    }

    holder.itemTitle.setText(items.get(position).getTitle());;
    holder.itemHolder.setBackgroundDrawable(new BitmapDrawable(bmp));

items.get(position).getLink() is meant to get the link that is being displayed in a ListView , but even something like URL url = new URL("https://www.google.com") doesn't work. items.get(position).getLink()旨在获取正在ListView中显示的链接,但即使类似URL url = new URL("https://www.google.com")也不起作用。

Thanks. 谢谢。

your url is formatted without the protocol at the beginning try 您的网址在开始尝试时没有协议的格式格式化

url = new URL("http://"+items.get(position).getLink());

sometimes the url string may have special characters, in which case you need to encode it properly please see this . 有时url字符串可能包含特殊字符,在这种情况下,您需要对其进行正确编码,请参阅this And also, the url you posted in the comment is not an image. 另外,您在评论中发布的网址不是图片。

it is exception beacuse of url class 这是URL类的异常

add antoher catch like 添加其他捕获

catch (MalformedURLException e) {

            e.printStackTrace();
        }

只需单击alt + enter,然后导入try catch部分,这对我有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM