问题
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