的URLConnection的getContent()返回NULL [英] URLConnection getContent() return null

查看:337
本文介绍了的URLConnection的getContent()返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的方法来获取一个位图输入流:

 私人的InputStream的getInputStream(字符串urlString)抛出MalformedURLException的,IOException异常{
    网址URL =新的URL(urlString);
    URLConnection的连接;
    连接= url.openConnection();
    //connection.setUseCaches(true);
    对象响应= connection.getContent();
    如果(!(响应的instanceof的InputStream))
        抛出新IOException异常(URLConnection的反应不是的instanceof的InputStream);    返回(InputStream的)响应;
}

这在Android 2.3(GalaxyS2)的作品不错,但在Android 2.2(的GalaxyS)
响应= NULL。

远程URL是一个位图。

任何想法?


解决方案

 连接= url.openConnection();
connection.connect(); //< - 它,如果你加入这行工作?
对象响应= connection.getContent();

I use the following method to get a bitmap input stream:

    private InputStream getInputStream(String urlString) throws MalformedURLException, IOException {
    URL url = new URL(urlString);
    URLConnection connection;
    connection = url.openConnection();
    //connection.setUseCaches(true); 
    Object response = connection.getContent();
    if (!(response instanceof InputStream))
        throw new IOException("URLConnection response is not instanceof InputStream");

    return (InputStream)response;
}

It works great on Android 2.3 (GalaxyS2) but on Android 2.2 (GalaxyS) response = null.

The remote url is a bitmap.

Any ideas?

解决方案

connection = url.openConnection();
connection.connect(); // <-- does it work if you add this line?
Object response = connection.getContent();

这篇关于的URLConnection的getContent()返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆