1

此代码在 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);
    }
4

1 回答 1

0

您无法在较新版本的 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
     }
}
于 2013-01-30T16:34:10.857 回答