- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用Retrofit & OKHttp来缓存 HTTP 响应。但我得到java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()
在空对象引用上。在这里我将放置我的 API Java 类。请帮助我,我们将非常感谢您的帮助。
这是我的 ApiManager 类,其中包含拦截器,getAPI() 具有改造构建部分:
public class ApiManager {
private static Context context;
public static final String API_URL = "https://www.alot.com/webservice/"; //Production
private static ProductionAPIService service;
public ProductionAPIService getService() {
return service;
}
private static OkHttpClient okClient;
private static ApiManager apiManager = null;
private ApiManager() {
}
public static ApiManager getApi() {
if (apiManager == null) {
apiManager = new ApiManager();
File httpCacheDirectory = new File(context.getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
// .client(SelfSigningClientBuilder.createClient(MyApplication.context))
.client(client)
.build();
service = retrofit.create(ProductionAPIService.class);
}
return apiManager;
}
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (AppUtils.isConnectedToInternet(context,true)) {
int maxAge = 60; // read from cache for 1 minute
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}
};
}
最佳答案
当分配给变量的内存中没有对象时,会抛出
NullPointerException
。换句话说,您没有将任何 Context
分配给您的 context
变量。您应该将其传递到静态 getApi()
方法中。最终结果应该是这样的
public static ApiManager getApi(@NonNull Context context) {
if (apiManager == null) {
apiManager = new ApiManager();
this.context = context;
File httpCacheDirectory = new File(context.getCacheDir(), "responses");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
// .client(SelfSigningClientBuilder.createClient(MyApplication.context))
.client(client)
.build();
service = retrofit.create(ProductionAPIService.class);
}
return apiManager;
}
关于java.lang.NullPointerException :ava. io.File android.content.Context.getCacheDir()' on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56921115/
我正在尝试在应用程序缓存文件夹中创建子目录,但在尝试检索文件时我什么也没得到。我下面有一些关于如何创建子目录以及如何从中读取内容的代码,也许我只是做错了什么(很明显我哈哈)或者也许这是不可能的? (虽
当我调试时,我发现 GetCacheDir 返回 /data/data/yourapp/cache/ 但是当我存储文件时,我在该路径中没有发现任何东西(使用 shell),但是在 /sdcard/An
我想下载从 Dropbox 下载的图像并缓存它们以供进一步使用: String cachePath = mContext.getCacheDir().getAbsolutePath() + entry
我只能在扩展 Activity 的类中使用 getCacheDir() 吗?我想在 AsyncTask 中使用它,这样我就可以在其中保存时间密集型缓存文件。 谢谢克里斯 最佳答案 将其传递给您的 As
在此链接中:http://developer.android.com/training/volley/requestqueue.html 标题:设置网络和缓存 它像这样定义缓存: Cache cach
我通过 class 创建了一个 extends Activity 的文件,现在我想从另一个 extends Fragment 的类打开这个文件。 但显然不可能从未扩展 Activity 的 class
我正在使用以下代码在 Android 上保存和检索图像文件 (JPG): File cacheDir = context.getCacheDir(); 这些文件什么时候被删除?据我所知,这个目录并不可
我正在尝试以编程方式清除我的应用程序数据。但是我得到了一个带有以下代码的 NullPointerEXception。 这是我的代码:- public class MyApplication exten
一旦系统内存不足,Android 可能会自动删除 CacheDir 中的文件。但是文档说我们不应该依赖系统清除这个缓存,因此编写额外的代码来进行轮询和删除。 如果是这样,为什么要选择 getCache
我在应用程序中有一些可下载的内容(例如:游戏角色图像),在iOS中将它们保存到NSCachesDirectory路径中,我遵循iOS的准则,即应将可下载的内容保存在NSCachesDirectory中
Android 不允许在 cacheDir 中创建子目录吗?鉴于第三方(例如供应商)肯定需要缓存他们自己的文件,因此为我的缓存文件创建一个子目录并让我集成的第三方管理他们自己的缓存文件对我来说很有意义
context.getCachedir() 和 Environment getConDownloadCacheDirectory 是否相同? 我正在实现位图缓存,但不知道该使用哪一个。 如果我使用上下
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 4 年前。 我正在尝试使用Retrofit
我是一名优秀的程序员,十分优秀!