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

发布于 2024-11-02 06:58:05 字数 1041 浏览 0 评论 0原文

当尝试在android 2.1平台上打开连接时,HttpUrlConnection.getInputStream方法总是抛出IOException、FileNotFound异常。该代码在 Android 2.2 和 2.3 版本中按预期工作。我有一个创建新连接的标准方法。当调用获取输入流时,错误会立即抛出。请注意,尝试建立的所有连接都是“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;
    }

When attempting to open a connection on the android 2.1 platform, the HttpUrlConnection.getInputStream method always throws a IOException, FileNotFound exception. The code works as expected in 2.2 and 2.3 version of Android. I have a standard method for creating a new connection. The error is thrown immediately after this when get input stream is called. As a note, all of the connections attempting to be made are "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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-11-09 06:58:05

如果它在 2.1 上打开连接 - 我看不出它不应该在 > 上工作的原因2.1.

可能是您的 Android.manifest 中缺少权限。如果是,您是否尝试过这个

If it opens the connection on 2.1 - I don't see a reason why it shouldn't work on > 2.1.

Could it be a missing permission in your Android.manifest. If yes, have you tried this ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文