Java HttpResponseCache可以使用图像,但不能使用Json
我正在尝试使用HttpResponseCache为web请求启用响应缓存。在我的应用程序中,我通过UrlConnection和Loader执行请求,然后将InputStream结果发送给活动以显示图像或使用JsonObject。在我的例子中,HttpResponseCache使用图像的url,并且可以在脱机时显示图像,但是使用Json的url,inputstream结果只在联机时工作,它在脱机时无法显示数据 装载机类别:Java HttpResponseCache可以使用图像,但不能使用Json,java,android,json,caching,httpresponsecache,Java,Android,Json,Caching,Httpresponsecache,我正在尝试使用HttpResponseCache为web请求启用响应缓存。在我的应用程序中,我通过UrlConnection和Loader执行请求,然后将InputStream结果发送给活动以显示图像或使用JsonObject。在我的例子中,HttpResponseCache使用图像的url,并且可以在脱机时显示图像,但是使用Json的url,inputstream结果只在联机时工作,它在脱机时无法显示数据 装载机类别: public InputStream loadInBackground()
public InputStream loadInBackground() {
InputStream streamResult = null;
HttpURLConnection conn = null;
URL url = new URL(urlConnection);
conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(true);
final InputStream finalStream = conn.getInputStream();
streamResult = finalStream;
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
ShowData(finalStream);
}
});
return streamResult;
}
活动:
@Override
public void ShowData(InputStream output) {
if (output != null) {
if (rbtImage.isChecked()) {
bitmap = BitmapFactory.decodeStream(output);
} else if (rbtJson.isChecked()) {
try {
result = convertStreamToString(output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在加载器执行后,我可以通过图像的Url显示带有该位图的图像。但脱机时无法使用Json的Url显示数据
我尝试使用:addRequestPropertyCache控件,仅在缓存时使用;
最大过期,最大使用年限,最大使用年限=600,私人,必须重新验证。但什么都不管用
请帮助我了解如何将HttpResponseCache与Json一起使用