OkHttp + Picasso + Retrofit

允我心安 提交于 2019-12-04 09:53:33

问题


The question is how to combine all these 3 libraries in one project?

  • Make one OkHttpClient to be a background layer for both Picasso and Retrofit.
  • How to make Priority changes like in Volley lib. (for pagination)?

回答1:


In a nutshell:

OkHttpClient okHttpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(okHttpClient)).build();
OkHttpDownloader downloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(this).downloader(downloader).build();

I do not think it's possible to have priorities with the current version of Retrofit.




回答2:


For OkHttpClient 3.0 and Retrofit 2.0 it is:

OkHttpClient client = new OkHttpClient.Builder()
    .cache(cache) // optional for adding cache
    .networkInterceptors().add(loggingInterceptor) // optional for adding an interceptor
    .build();

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://api.yourdomain.com/v1/")
    .addConverterFactory(GsonConverterFactory.create())
    .client(client)
    .build();

Picasso picasso = Picasso.Builder(context)
    .downloader(new OkHttp3Downloader(client))
    .build();

Prioritization has been moved down the stack model to the http client, and there is an issue being studied: https://github.com/square/okhttp/issues/1361



来源:https://stackoverflow.com/questions/23831986/okhttp-picasso-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!