我建议您看一下ASyncTask类(自Android
1.5起可用)。
它简化了创建后台线程的过程,该后台线程在完成后便与GUI线程同步。
您应该能够使用代码列出的内容来实现您正在尝试的功能
private class DownloadFilesTask extends AsyncTask<String, List<Product>, Integer> { protected List<Products> doInBackground(String... requestStrings) { int count = requestStrings.length; int results = 0; for (int i = 0; i < count; i++) { String requestString = requestStrings[i]; HttpGet httpGet = new HttpGet(requestString); httpGet.addHeader("Accept", "text/xml"); String encodingString = "testuser:testpass"; String sEnpredString = base64Coder.enpreString(encodingString); try{ String sContent = fetchURL(requestString, sEnpredString); XMLParser xmlParser = new XMLParser(); List <Product> products = xmlParser.getProducts(sContent); results++; publishProgress(products); } catch(Exception ex){ Log.e(TAG, ex.getMessage()); } } return results; } protected void onProgressUpdate(Integer... progress) { // TODO You are on the GUI thread, and the first element in // the progress parameter contains the last progress // published from doInBackground, so update your GUI } protected void onPostExecute(int result) { // Processing is complete, result contains the number of // results you processed } }并通过调用执行
new DownloadFilesTask().execute(url1, url2, url3);



