我为检查网址编写了这个方法。
public boolean checkresume(String URL) throws IOException{
HttpURLConnection urlc;
urlc = (HttpURLConnection)new URL(url).openConnection();
urlc.setRequestProperty("Range", "bytes="+1+"-");
urlc.getResponseCode();
Log.e("Check Resume",urlc.getHeaderField("Accept-Ranges").toString());
if((urlc.getHeaderField("Accept-Ranges").equals("bytes"))){
return true;
}else{
return false;
}
}
导入具有继续URL功能的URL时,此方法没有错误,返回" true"。
但是,当导入无法继续URL的URL时,崩溃的应用程序并返回此日志
网址示例:https://codeload.github.com/libuchao/Dictionary/zip/master
日志:
08-01 13:25:12.201: E/AndroidRuntime(8466): FATAL EXCEPTION: main
08-01 13:25:12.201: E/AndroidRuntime(8466): Process: com.yyxu.download, PID: 8466
08-01 13:25:12.201: E/AndroidRuntime(8466): java.lang.RuntimeException: Unable to start service com.yyxu.download.services.DownloadService@429f1b38 with Intent { act=com.yyxu.download.services.IDownloadService (has extras) }: java.lang.NullPointerException
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2877)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.app.ActivityThread.access$2200(ActivityThread.java:161)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.os.Handler.dispatchMessage(Handler.java:102)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.os.Looper.loop(Looper.java:157)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-01 13:25:12.201: E/AndroidRuntime(8466): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 13:25:12.201: E/AndroidRuntime(8466): at java.lang.reflect.Method.invoke(Method.java:515)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-01 13:25:12.201: E/AndroidRuntime(8466): at dalvik.system.NativeStart.main(Native Method)
08-01 13:25:12.201: E/AndroidRuntime(8466): Caused by: java.lang.NullPointerException
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.yyxu.download.services.DownloadTask.checkresume(DownloadTask.java:92)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.yyxu.download.services.DownloadTask.<init>(DownloadTask.java:76)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.yyxu.download.services.DownloadManager.newDownloadTask(DownloadManager.java:517)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.yyxu.download.services.DownloadManager.newDownloadTask(DownloadManager.java:342)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.yyxu.download.services.DownloadManager.addTask(DownloadManager.java:86)
08-01 13:25:12.201: E/AndroidRuntime(8466): at com.yyxu.download.services.DownloadService.onStart(DownloadService.java:58)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.app.Service.onStartCommand(Service.java:450)
08-01 13:25:12.201: E/AndroidRuntime(8466): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2860)
08-01 13:25:12.201: E/AndroidRuntime(8466): ... 10 more
问题是com.yyxu.download.services.DownloadTask.checkresume(DownloadTask.java:92)
第92行是:Log.e("Check Resume",urlc.getHeaderField("Accept-Ranges").toString());
请帮帮我。
我很抱歉我说英语。
感谢。
答案 0 :(得分:0)
getHeaderField(String name)
返回named的值
标头字段,如果标头中没有此类字段,则为null
。此方法返回String
,因此无需使用toString()
。
这仅表示您的URLConnection
没有名为Accept-Ranges
的标题字段。确保您使用标题的确切名称,因为它区分大小写。
您还可以使用HttpURLConnection
方法从数字getHeaderField(int n)
获取标头返回nth
标头。但更重要的是,您应该在使用它之前进行null
检查以避免NPE
这样:
if(urlc.getHeaderField("Accept-Ranges") != null) {
// ... Move ahead