HttpURLConnection.getInputStream在Android 2.1中总是抛出IOException

4

在安卓2.1平台上尝试打开连接时,HttpUrlConnection.getInputStream方法总是会抛出一个IOException、FileNotFound异常。但在安卓2.2和2.3版本中,该代码可以正常工作。我有一个用于创建新连接的标准方法,在调用get input stream后立即抛出错误。需要注意的是,所有尝试建立的连接都是“http://something/something”。

public static URLConnection createConnection(String urlStr, Boolean useAuthentication, Boolean setOutput){
        Log.i(GpodRoid.LOGTAG, "Creating Connection to " + urlStr);
        URLConnection conn = null;
        try {
            conn = new URL(urlStr).openConnection();
            conn.setDoOutput(setOutput);
            conn.setDoInput(true);

            if(useAuthentication){
                String auth = GpodRoid.prefs.getUsername() + ":" + GpodRoid.prefs.getPassword();
                String encoded = Base64.encodeBytes(auth.getBytes());
                conn.setRequestProperty("Authorization","basic " + encoded);
            }
        } 
        catch (Exception e) {
            Log.e(GpodRoid.LOGTAG, "Error creating Connection: " + stackTrace(e));
        } 

        return conn;
    }
1个回答

1

如果它在2.1上打开连接,我不认为在> 2.1上工作没有理由。

你的Android.manifest中是否缺少权限?如果是,你尝试过this了吗?


1
好的。如果你找到了解决方案,请发布出来,这样其他人也可以受益。谢谢! - Sagar Hatekar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接