0

I'm getting a MalformedURLException some code in my Android Studio project. 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.

I have already put the code in a try,catch, but that is still giving me the error. 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.

Thanks.

Groot
  • 483
  • 1
  • 9
  • 20
  • can you do a Log.e("urltest", items.get(position).getLink())` to see the full url you pass to `decodeStream` – ashoke Oct 04 '14 at 22:12
  • Thanks for the reply, I just did that and the URL seems to be looking fine. I'm trying to make a news application so here is the link that one of the list items outputted: http://www.pcworld.com/article/2691912/jpmorgan-chase-attackers-hacked-other-banks-report-says.html#tk.rss_all – Groot Oct 04 '14 at 22:16

3 Answers3

1

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. And also, the url you posted in the comment is not an image.

Community
  • 1
  • 1
ashoke
  • 6,441
  • 2
  • 26
  • 25
  • Sorry @ashoke, but stackoverflow is removing it when I post the link. It does have the http protocol at the start – Groot Oct 04 '14 at 22:27
0

it is exception beacuse of url class

add antoher catch like

catch (MalformedURLException e) {

            e.printStackTrace();
        }
Aakash Gandhi
  • 253
  • 1
  • 3
  • 11
0

Just click alt+enter and then import try catch section... this helped me...

  • You need to add more details of exactly what you did, such as which exception you used, what happened that solved the problem, etc. – cst1992 May 19 '16 at 07:09