将代码更新为4.1

时间:2013-01-30 16:25:25

标签: java android httpurlconnection

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

1 个答案:

答案 0 :(得分: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
     }
}