最近在学习安卓,mars的视频教程。里面有一集是从服务器下载文件,遇到了一个问题:
下载函数public class HttpDownloader {
public URL url = null;

/*
 * public String download(String urlStr) 下载文本文档
 */
public String download(String urlStr){
StringBuffer sb = new StringBuffer();
String str = null;
BufferedReader buffer = null;
try{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
//获得输入流,是字节流,由InputStreamReader转换成字符流,放入BufferedReader中,以便后面用readline方法。
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((str = buffer.readLine()) != null){
sb.append(str);
}
}catch(Exception e){ //打印异常
e.printStackTrace();
}finally{ //关闭
try {
buffer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}

/*
 * 该函数返回整形 :
 * -1:代表下载文件出错  
 * 0:代表下载文件成功 
 * 1:代表文件已经存在
 */
public int downFile(String urlStr,String path,String fileName){
InputStream inputStream = null;
try {
FileUtils fileUtils = new FileUtils();

if (fileUtils.isFileExist(path + fileName)) {
return 1;
} else {
inputStream = geturlStrInputStream(urlStr);
File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);
if (resultFile == null) {
return -1;
}
}
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
} public InputStream geturlStrInputStream(String urlStr){
InputStream inputstream = null;
try{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
inputstream = urlConn.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return inputstream;
}
}下面是监听器的代码
public void onClick(View arg0) {
// TODO Auto-generated method stub
HttpDownloader hd = new HttpDownloader();
System.out.println("aaaaaaaaaaa");
String result = hd.download("http://localhost:8080/mp3/resources.xml");
System.out.println(result);
System.out.println("bbbbbbbbbbb");
try{
//创建一个SAXParserFactory
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();
//为XMLReader设置内容处理器
reader.setContentHandler(new myContentHandler());
//开始解析文件
reader.parse(new InputSource(new StringReader(result)));
}
catch(Exception e){
e.printStackTrace();
}
}
    
后面解析不是问题,只是说开始就没有下载到但是下载url地址是正确的,因为用downFile(String urlStr,String path,String fileName)这个函数就可以下载并保存到sd卡,但是用download这个函数,最后也没有System.out输出求解答啊 啊   啊