问题:做android的MP3项目下载MP3文件的时候,第一次成功,然后再次下载就报错。为什么?
log:06-05 10:49:11.340: I/System.out(13586): file--->/mnt/sdcard/mp3/a2.mp3
06-05 10:49:15.820: D/dalvikvm(349): GC_EXPLICIT freed 8K, 55% free 2591K/5703K, external 1625K/2137K, paused 367ms
06-05 10:49:17.122: I/System.out(13586): result---->0
06-05 10:49:20.540: D/dalvikvm(386): GC_EXPLICIT freed 1K, 55% free 2533K/5511K, external 1625K/2137K, paused 54ms
06-05 10:49:23.840: W/dalvikvm(13586): threadid=9: thread exiting with uncaught exception (group=0x40015560)
06-05 10:49:23.850: E/AndroidRuntime(13586): FATAL EXCEPTION: Thread-11
06-05 10:49:23.850: E/AndroidRuntime(13586): java.lang.NullPointerException
06-05 10:49:23.850: E/AndroidRuntime(13586):  at napo.download.HttpDownloader.downFile(HttpDownloader.java:66)
06-05 10:49:23.850: E/AndroidRuntime(13586):  at napo.mp3player.service.DownloadService$DownloadThread.run(DownloadService.java:38)
06-05 10:49:23.850: E/AndroidRuntime(13586):  at java.lang.Thread.run(Thread.java:1019)
06-05 10:49:23.870: W/ActivityManager(76):   Force finishing activity napo.mp3player/.MainActivity
06-05 10:49:31.021: D/dalvikvm(258): GC_EXPLICIT freed 6K, 54% free 2537K/5511K, external 1625K/2137K, paused 85ms
06-05 10:49:44.810: I/Process(13586): Sending signal. PID: 13586 SIG: 9
06-05 10:49:44.830: I/ActivityManager(76): Process napo.mp3player (pid 13586) has died.
06-05 10:49:44.830: W/ActivityManager(76): Scheduling restart of crashed service napo.mp3player/.service.DownloadService in 5000ms
06-05 10:49:44.890: W/InputManagerService(76): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@407c5e78
06-05 10:49:49.931: I/ActivityManager(76): Start proc napo.mp3player for service napo.mp3player/.service.DownloadService: pid=13746 uid=10034 gids={1015, 3003}
06-05 10:50:20.686: D/SntpClient(76): request time failed: java.net.SocketException: Address family not supported by protocol
代码:
/**
 * 返回0代表下载成功,返回1代表已经有此文件,返回-1代表下载出错。
 * @param urlStr
 * @param path
 * @param fileName
 * @return
 */
public int downFile(String urlStr, String path, String fileName) {
InputStream inputStream = null;

try{
FileUtils fileUtils = new FileUtils();
if (fileUtils.isFileExist(fileName, path)){
return 1;
} else {

inputStream = getInputStreamFromUrl(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 (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;

}

public InputStream getInputStreamFromUrl(String urlStr) throws IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;

}

解决方案 »

  1.   

    你下载完一首歌后inputStream.close();...直接关闭了~
      

  2.   

    HttpDownloader.java 文件发上来看一下,存在空指针
      

  3.   

    package napo.download;import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;import napo.utils.FileUtils;public class HttpDownloader {
    public URL url = null;

    public String download(String urlStr) {
    StringBuffer sb = new StringBuffer();
    String line = null;
    BufferedReader buffer = null;
    try{
    url = new URL(urlStr);
    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
    buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    while ((line = buffer.readLine()) != null) {
    sb.append(line);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try{
    buffer.close();
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    return sb.toString();
    }

    /**
     * 返回0代表下载成功,返回1代表已经有此文件,返回-1代表下载出错。
     * @param urlStr
     * @param path
     * @param fileName
     * @return
     */
    public int downFile(String urlStr, String path, String fileName) {
    InputStream inputStream = null;

    try{
    FileUtils fileUtils = new FileUtils();
    if (fileUtils.isFileExist(fileName, path)){
    return 1;
    } else {

    inputStream = getInputStreamFromUrl(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 (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return 0;

    }

    public InputStream getInputStreamFromUrl(String urlStr) throws IOException {
    url = new URL(urlStr);
    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
    InputStream inputStream = urlConn.getInputStream();
    return inputStream;

    }
    }
      

  4.   

    在inputStream.close();前面加一行
    if(inputStream !=null)