Android java.IOException:索引0处主机名中的非法字符

时间:2011-09-13 17:36:20

标签: java android

我想知道是否有人可以帮助我。这是关于android开发的。

我正在使用此图片链接。 http://soompi_images.s3.amazonaws.com/4b1d3d685350b3ee612d098fda7e7441_large.jpg

我使用inputstream获取图像以获取图像内容。

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

这是为了从我的drawable的图像链接获取图像。

但是,我在索引0的主机名处收到非法字符。

我尝试过htmlEncode,但它不起作用。所以我希望有人可以帮助我。

感谢。

4 个答案:

答案 0 :(得分:3)

Java URL类无法解析其中包含下划线的主机名,例如* soompi_images * Pheonixblade9是一个非常好的工作。

答案 1 :(得分:1)

这就是我的所作所为:

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;
}

希望这有帮助!

答案 2 :(得分: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();

答案 3 :(得分:0)

真正的答案是不要在您的网址中使用无效的主机名。您无法指望任何无效的主机名可靠地运行。

  

协议的Internet标准(请求注释)要求组件主机名标签只能包含ASCII字母' a'通过' z' (以不区分大小写的方式),数字' 0' 0通过' 9'和连字符(' - ')。 RFC 952中主机名的原始规范,强制标签不能以数字或连字符开头,并且不得以连字符结尾。但是,后续规范(RFC 1123)允许主机名标签以数字开头。不允许使用其他符号,标点字符或空格。   <

有效主机名的来源:

RFC 1123,RFC 952,Wikipedia