URLConnection getContent()返回null

时间:2011-11-18 08:31:39

标签: android inputstream urlconnection

我使用以下方法获取位图输入流:

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

它在Android 2.3(GalaxyS2)上运行良好但在Android 2.2(GalaxyS)上运行良好 response = null。

远程网址是位图。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

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