gpt4 book ai didi

java.lang.NullPointerException :ava. io.File android.content.Context.getCacheDir()' on null

转载 作者:行者123 更新时间:2023-12-02 03:22:11 24 4
gpt4 key购买 nike

我正在尝试使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com