【发布时间】:2018-01-11 18:16:34
【问题描述】:
在进行更多研究后,我现在能够澄清我的问题。我有一个代码段,我知道它可以与我尝试使用的所有其他文件一起使用,除了一个文本文件。代码检查我使用 URL 和 HTTPURLConnection 发送的文件。我使用addRequestProperty("Range", "bytes=0-0") 在服务器上获取足够的文件,这样我就可以检查 LastModified 字段而无需再次下载文件。这是检查文件的代码:
private void getFileDetails() throws IOException, ParseException {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.addRequestProperty("Range", "bytes=0-0");
con.connect();
String cr = con.getHeaderField("Content-Range");
URL url2 = new URL("url for json file");
URL url3 = new URL("url for txt file");
Map<String, List<String>> map = con.getHeaderFields();
if(url.equals(url2) || url.equals(url3)) {//This is just for seeing if it added Range
Log.i("HTTP", url.toString());
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
Log.i("HTTP", "Key : " + entry.getKey() +
" ,Value : " + entry.getValue());
}
Log.i("HTTP", "----------------------------------------------------------\n");
}
if (cr == null)//Fails Right here
throw new IOException();
length = Long.parseLong(cr.substring(cr.indexOf('/')+1, cr.length()));
String date = con.getHeaderField("Last-Modified");
String pattern = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
Date javaDate = format.parse(date);
lastModified = javaDate.getTime();
}
这是我对有效文件字段的测试输出:
I/HTTP: Key : null ,Value : [HTTP/1.1 206 Partial Content]
I/HTTP: Key : Accept-Ranges ,Value : [bytes]
I/HTTP: Key : Connection ,Value : [Keep-Alive]
I/HTTP: Key : Content-Length ,Value : [1]
I/HTTP: Key : Content-Range ,Value : [bytes 0-0/15431]
I/HTTP: Key : Content-Type ,Value : [application/json]
I/HTTP: Key : Date ,Value : [Thu, 11 Jan 2018 17:54:00 GMT]
I/HTTP: Key : ETag ,Value : ["3c47-560c8525e5d0b"]
I/HTTP: Key : Keep-Alive ,Value : [timeout=5, max=100]
I/HTTP: Key : Last-Modified ,Value : [Wed, 20 Dec 2017 16:46:15 GMT]
I/HTTP: Key : Server ,Value : [Apache/2.4.18 (Ubuntu)]
I/HTTP: Key : X-Android-Received-Millis ,Value : [1515693240969]
I/HTTP: Key : X-Android-Response-Source ,Value : [NETWORK 206]
I/HTTP: Key : X-Android-Selected-Protocol ,Value : [http/1.1]
I/HTTP: Key : X-Android-Sent-Millis ,Value : [1515693240804]
现在这里是文本文件不起作用的结果:
I/HTTP: Key : null ,Value : [HTTP/1.1 200 OK]
I/HTTP: Key : Accept-Ranges ,Value : [bytes]
I/HTTP: Key : Connection ,Value : [Keep-Alive]
I/HTTP: Key : Content-Type ,Value : [text/plain]
I/HTTP: Key : Date ,Value : [Thu, 11 Jan 2018 17:55:44 GMT]
I/HTTP: Key : ETag ,Value : ["1370000-560b66148adf5-gzip"]
I/HTTP: Key : Keep-Alive ,Value : [timeout=5, max=100]
I/HTTP: Key : Last-Modified ,Value : [Tue, 19 Dec 2017 19:21:56 GMT]
I/HTTP: Key : Server ,Value : [Apache/2.4.18 (Ubuntu)]
I/HTTP: Key : Transfer-Encoding ,Value : [chunked]
I/HTTP: Key : Vary ,Value : [Accept-Encoding]
I/HTTP: Key : X-Android-Received-Millis ,Value : [1515693344544]
I/HTTP: Key : X-Android-Response-Source ,Value : [NETWORK 200]
I/HTTP: Key : X-Android-Selected-Protocol ,Value : [http/1.1]
I/HTTP: Key : X-Android-Sent-Millis ,Value : [1515693344397]
您可以看到文本文件标题中缺少 Content-Range 字段,并且状态消息是 OK 而不是 Partial Content。我知道,因为我得到了cr == null 的真实值,这意味着它在文本文件的头文件中不存在。我不明白添加出了什么问题。我到处寻找人们遇到类似问题的地方,但整个事情都行不通。任何帮助,将不胜感激。
编辑:好的,所以真正的问题是文本文件没有长度字段。它检查 LastModified 和文件的大小以查看是否需要更新。
编辑:我现在知道出了什么问题。文本文件是分块编码的,没有Content-Length
【问题讨论】:
-
您是否使用 ETags 来确定这些资源更改?
-
是的,在某种程度上。我弄清楚了最近发现的问题是如何解决我遇到的问题。我会发布答案并解释更多发生的事情。
标签: java android android-studio httpurlconnection