【问题标题】:IOException with URLConnection and getInputStream带有 URLConnection 和 getInputStream 的 IOException
【发布时间】:2012-04-20 22:11:52
【问题描述】:

我正在尝试从带有 android 应用程序的网站获取数据。

这是我的代码:

URL url = new URL("http://www.google.com/ig/api?weather=Schriesheim,Germany");
URLConnection uCon = url.openConnection();
iStream = uCon.getInputStream();

getInputStream() 方法返回一个 IOException,我已经尝试了很多在 Internet 上找到的东西,但没有任何效果。 顺便说一句,我在 Java 应用程序中尝试了这段代码并且它工作正常,所以我认为问题出在我的 Android 设置中。

我已经在我的 Manifest.xml 中添加了这些行:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

我正在使用 Eclipse 并运行 Archlinux x64。 有什么想法吗?

谢谢。

【问题讨论】:

  • 有什么解决方案适合你吗?

标签: java android inputstream httpurlconnection


【解决方案1】:

您可以尝试使用此功能从互联网获取数据

        public static String get(String from) {
            try {
                HttpClient client = new DefaultHttpClient();  
                HttpGet get = new HttpGet(from);
                HttpResponse responseGet = client.execute(get);  
                HttpEntity resEntityGet = responseGet.getEntity();  
                if (resEntityGet != null) return EntityUtils.toString(resEntityGet);
                } catch (Exception e) {
                    Log.e("GET", e.toString());
                }
            return null;
        }

【讨论】:

  • 感谢您的帮助。但我得到一个 IO 异常:responseGet = client.execute(get); 我真的认为这是一个 android 配置问题,但我无法弄清楚..
【解决方案2】:

试试

uCon.setDoInput(true);

之前

uCon.getInputStream();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多