此代码在 2.2 模拟器(API 8)中工作正常,但在 4.1 模拟器(API 16)。有什么想法吗?
感谢您的所有回答和兴趣
`try
{
String url = "http://10.0.2.2:8080/cofradeapps/static/hermandades.xml";
urlHermandades = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) urlHermandades.openConnection();
InputStream in = urlConnection.getInputStream();
return in;
}
catch (IOException e)
{
throw new RuntimeException(e);
}
最佳答案
您无法在较新版本的 Android 上的 UI 线程上进行网络连接,您需要使用另一个线程,很可能是 ASyncTask。
private class YourClass extends AsyncTask<URL> {
protected String doInBackground(URL... urls) {
//Do your stuff here
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
//When you're finished update the UI thread with your results
}
}
关于java - 更新代码至4.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14608813/