2012-04-07 47 views
1

我試圖從一個網站與android應用程序獲取數據。IOException與URLConnection和getInputStream

這裏是我的代碼:

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

方法的getInputStream()返回我一個IOException,我已經嘗試過很多事情找到在互聯網上,但沒有什麼工作。 順便說一下,我在Java應用程序中試過這段代碼,它工作,所以我認爲這個問題出現在我的Android設置中。

我已經在我的Manifest.xml加上這些行:

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

我使用Eclipse和運行的Archlinux 64。 有什麼想法?

謝謝。

+0

這一個爲你工作的任何解決方案? – Hosni 2013-05-10 04:16:16

回答

2

你可以試試這個功能用於獲取數據從互聯網

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

謝謝你的幫助。但我得到一個IO異常: 'responseGet = client.execute(get); ' 我真的認爲這是一個Android配置問題,但我無法弄清楚.. – user1319550 2012-04-08 17:14:42

0

嘗試

uCon.setDoInput(true); 

uCon.getInputStream();