gpt4 book ai didi

android - 尝试使用 httpCache android?

转载 作者:行者123 更新时间:2023-11-29 16:02:45 25 4
gpt4 key购买 nike

我正在尝试使用 okhttpclient 进行改造以设置缓存...如何设置到期并将其添加到我的 restApiManager?不确定应该是什么服务...

代码如下:

public class ApiManager {

private static final String API_URL = "ip";


public static AsynchronousApi getInstance() {
if(service == null) {
OkHttpClient okHttpClient = new OkHttpClient();
File cacheDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
HttpResponseCache cache = null;
try {
cache = new HttpResponseCache(cacheDir, 1024);
} catch (IOException e) {
e.printStackTrace();
}
okHttpClient.setResponseCache(cache);

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(okHttpClient))
.setErrorHandler(new ErrorHandler() {

@Override
public Throwable handleError(RetrofitError arg0) {
if(arg0.getResponse().getStatus() == 404)
return new Exception("Url does not exists");

return new Exception(arg0.getMessage());
}
})
.build();
service = restAdapter.create(AsynchronousApi.class);
}

return service;
}



// //create adapter
private static final AsynchronousApi ASYNCHRONOUS_API = getInstance();

//call service to initiate
public static AsynchronousApi getAsyncApi() {
return ASYNCHRONOUS_API;
}

//Call interface
public interface AsynchronousApi {

//USER

//Register User
@FormUrlEncoded
@POST("/register")
public void registerUser(
@Field("email") String email,
@Field("username") String username,
@Field("password") String password,
Callback<UserResponse> callback); //


//Search User
@GET("/search_user")
public void searchUser(
@Query("username") String username,
Callback<UserResponse> callback); // userfound cache users

最佳答案

您正在使用 com.squareup.okhttp.OkHttpClient,但您应该使用以 OkHttpClient 作为参数的 retrofit.client.OKClient。此外,如果 okhttp jar 已经在您的路径中,您根本不需要使用此代码。只有在您面临缓存可用性问题时才有可能。

 private static RestAdapter REST_ADAPTER = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(okHttpClient))
.build();

关于android - 尝试使用 httpCache android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22445177/

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