我有以下方法:
public void GetBackground(final int width, final int height, final Long ifModifiedSince,
final GetBackgroundListener listener)
{
new Thread(new Runnable()
{
public void run()
{
try {
String urlString = serviceUri + "/DownloadBackground?width=" + width
+ "&height=" + height;
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
if (ifModifiedSince != null) {
Log.d(TAG, "Background last modified: " + ifModifiedSince);
connection.setIfModifiedSince(ifModifiedSince);
}
InputStream stream = (InputStream)connection.getContent();
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
listener.onNotModified();
Log.d(TAG, "Background not modified");
} else {
Bitmap background = BitmapFactory.decodeStream(stream);
listener.onSuccess(background, connection.getLastModified());
Log.d(TAG, "New background");
}
} catch (Exception e) {
listener.onError(e);
}
}
}).start();
}
在 Android 2.2 上,它会生成以下包:
GET /QurrentService.svc/DownloadBackground?width=480&height=480 HTTP/1.1
if-modified-since: Thu, 13 Dec 2012 09:11:23 GMT
User-Agent: Dalvik/1.2.0 (Linux; U; Android 2.2; sdk Build/FRF91)
Host: qurrentfeed1.1.aliencms.nl
Connection: Keep-Alive
在 Android 4.1 上:
GET /QurrentService.svc/DownloadBackground?width=800&height=800 HTTP/1.1
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.1.2; sdk Build/MASTER)
Host: qurrentfeed1.1.aliencms.nl
Connection: Keep-Alive
Accept-Encoding: gzip
If-Modified-Since: Thu, 13 Dec 2012 09:11:23 UTC
我不确定是否允许将 If-modified-since 日期作为 UTC 时间发送,但服务器无法解析日期。有没有办法强制 HttpURLConnection 生成 GMT 格式?或者我应该像这样手动设置标题?
String ifModifiedSinceString = DateUtils.formatDate(new Date(ifModifiedSince), "EEE, d MMM yyyy HH:mm:ss 'GMT'");
connection.addRequestProperty("If-Modified-Since", ifModifiedSinceString);