2

I wonder if anyone can help me on this. It's about android development.

i am using this image link for example. http://soompi_images.s3.amazonaws.com/4b1d3d685350b3ee612d098fda7e7441_large.jpg

I get the image using inputstream to get the image content.

InputStream is = 
   (InputStream) new URL("http://soompi_images.s3.amazonaws.com/4b1d3d685350b3ee612d098fda7e7441_large.jpg")
    .openStream();

This is to get the image from the image link for my drawable.

However, i am getting Illegal Character at hostname at index 0.

I have tried htmlEncode but it does not work. So i hope someone can help me.

Thanks.

4 Answers 4

3

The Java URL-class cannot parse hostnames with underscores in it, such as *soompi_images* Pheonixblade9 is a really good work around.

Sign up to request clarification or add additional context in comments.

4 Comments

how should i work around it? I tried changing the _ to ASCII code it also does not work.
The code below, posted by Pheonixblade9, works for me. Remove your code where you try to fetch the image and replace it with the code below.
The code work in the way that it does not show error because of the try and catch. However, the image still does not appear. I am looking for a way which i can get the image. Thanks anyway!
try stepping through the code. That's part of being a developer, debugging code that works for them to try to work in your situation.
1

This is what I do:

private Bitmap LoadImage(String URL, BitmapFactory.Options options) //
{
    Bitmap bitmap = null;
    InputStream in = null;
    try //
    {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } //
    catch (Exception ex) //
    {
        Logger.LogError("LoadImage", ex);
        return null;
    }
    return bitmap;
}

private InputStream OpenHttpConnection(String strURL) throws IOException //
{
    InputStream inputStream = null;
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();

    try //
    {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    } //
    catch (Exception ex) //
    {
        return null;
    }
    return inputStream;
}

Hope this helps!

10 Comments

I can't get the image with this. Thanks anyway!
what exactly isn't working about this? It works great for me. You do need to make sure you're doing this in the background, or it will freeze your app.
Yea it is running in the background. I mean i still can't get the image with it as i suppose is because of the underscore. However, i can't change the url as it is not by me.
What is the Exception being thrown by this? It works just fine with underscores in my app.
The exception thrown is java.lang.NullPointerException.
|
1
String url = "http://soompi_images.s3.amazonaws.com/4b1d3d685350b3ee612d098fda7e7441_large.jpg";

url = url.replace("soompi_images.s3.amazonaws.com", "s3.amazonaws.com/soompi_images");

InputStream is = (InputStream) new URL(url).openStream();

Comments

0

The real answer is to not use an invalid hostname in your URL. You cannot count on any invalid hostnames working reliably.

The Internet standards (Requests for Comments) for protocols mandate that component hostname labels may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9', and the hyphen ('-'). The original specification of hostnames in RFC 952, mandated that labels could not start with a digit or with a hyphen, and must not end with a hyphen. However, a subsequent specification (RFC 1123) permitted hostname labels to start with digits. No other symbols, punctuation characters, or white space are permitted. <

Sources for what is a valid host name:

RFC 1123, RFC 952, Wikipedia

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.