IOException with URLConnection and getInputStream

孤街浪徒 提交于 2019-12-13 00:33:27

问题


I'm trying to get data from a website with an android application.

Here is my code :

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

The method getInputStream() returns me an IOException, I already tried lots of things find on Internet, but nothing is working. By the way, I tried this code in a Java application and it worked so I assume this the problem is in my Android settings.

I already add these lines in my Manifest.xml :

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

I'm using Eclipse and running Archlinux x64. Any idea ?

Thank you.


回答1:


You can try this function for get data from internet

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



回答2:


Try

uCon.setDoInput(true);

before

uCon.getInputStream();


来源:https://stackoverflow.com/questions/10057942/ioexception-with-urlconnection-and-getinputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!