Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

retrofit - Trying to make use of httpCache android?

I am trying to use okhttpclient with retrofit to setup caching... How do I setup the expiration and add it to my restApiManager? Not sure what service is suppose to be...

Here is the code:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using the com.squareup.okhttp.OkHttpClient but you should rather use retrofit.client.OKClient which takes OkHttpClient as a parameter. Also if okhttp jar is already in your path you dont need to use this code at all. Only perhaps if you are facing trouble with cache availability.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...