问题:做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;
}
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;
}
解决方案 »
- 关于多线程下载的问题
- 用setImageResource方法动态设置ImageView的图片形成动画效果,可行吗?
- android2.2.2和android2.2 surface.cpp的疑问
- 运行android list命令出现下列错误
- 提问的技巧,请大家都看下吧
- skia画图求助
- android中title样式问题
- 启动AVD为出现这么多错误
- Volley用Json发送post请求
- 用Adobe animate as3开发 安卓客户端,如果不选全屏,刘海屏幕的手机下面会有一个黑条,还有如何获得除去状态栏的屏幕高度
- android开发中id不正确
- android平台下如何获得类似su权限,要求非云中漫步的作法
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;
}
}
if(inputStream !=null)