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)?
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.
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
Related
Android lower version devices (4.*) gives SSL error. Works perfectly on high versions of Android
I use a class that processes JSON requests, But in Android 4.4, no information about Json is loaded
public static ApiInterface createAPI() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30, TimeUnit.SECONDS);
builder.writeTimeout(30, TimeUnit.SECONDS);
builder.readTimeout(30, TimeUnit.SECONDS);
builder.cache(null);
OkHttpClient okHttpClient = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.'https://example.com/alljson')
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create(ApiInterface.class);
}
}
Please simply explain what I should do to make Jason load from HTTPS Addresses on all devices
Thanks all
Does anyone know in Android How to use Okhttpclient Create a Http Delete Or Put Method With Params?
Using java , this is what i have tried:
CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(context));
okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS) //其他配置
.build();
You can build a URL with query parameters using the HttpUrl class. Then you can use an okhttp3.Request.Buidler() along with either the post() or delete() methods:
HttpUrl url = new HttpUrl.Builder()
.host(host).addQueryParameter(name, value).build();
Request request = new Request.Builder()
.url(url).post(RequestBody.create(mediaType, body)).addHeader(type, header).build();
okhttpClient.newCall(request).enqueue(new Callback() {
...
});
You can check out the OkHttp wiki for recipes if you need further help. Or you could use Square's other wonderful library, Retrofit, which pairs well with OkHttp.
I am using picasso 2.5.2 library to download bitmap from remote server, the image url requires basic authentication in header.
i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries.
Answer - 1
Answer - 2
Answer - 3
Thanks in advance.
Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.authenticator(new Authenticator()
{
#Override
public Request authenticate(Route route, Response response) throws IOException
{
String credential = Credentials.basic("user", "pass");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();
Then, use that client in forming your Picasso object, but with okhttp3 you will have to use a OkHttp3Downloader instead, like so:
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(okHttpClient))
.build();
You can get the OkHttp3Downloader from https://github.com/JakeWharton/picasso2-okhttp3-downloader
I am caching the HTTP response Using Retrofit2 and OKHttp.
Here is my code:
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(application.getCacheDir(), cacheSize);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().
cache(cache).addNetworkInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
I am getting the response headers Cache-control and Expires from our backend REST API's.
Now i want to get the server Response by bypassing the the Expires Header.
Please help me with this problem.
Add this header to your HTTP request:
Cache-Control: no-cache
You can do this with Retrofit’s #Header or with an OkHttp interceptor.
I want to implement multiple parallel request in Retrofit 2.
I have the following structure to make 3 request :
HistoricalRApi.IStockChart service=HistoricalRApi.getMyApiService();
//^BVSP,^DJI,^IXIC
Call<HistoricalDataResponseTimestamp> call1= service.get1DHistoricalDataByStock("^IXIC");
Call<HistoricalDataResponseTimestamp> call2= service.get1DHistoricalDataByStock("^DJI");
Call<HistoricalDataResponseTimestamp> call3= service.get1DHistoricalDataByStock("^GSPC");
call1.enqueue(retrofitCallbackAmerica());
call2.enqueue(retrofitCallbackAmerica());
call3.enqueue(retrofitCallbackAmerica());
}
I have read that in Retrofit1, when defining the rest adapter one can define parallel request with .setExecutor like here:
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(END_POINT)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setExecutors(Executors.newFixedThreadPool(3), null)
.build();
My question is how can i achieve the same in Retrofit 2? Thanks in advance
Thanks to Colin Gillespie link i have implemented what Jake Wharton says and this is the result:
public static IStockChart getMyApiService() {
OkHttpClient client=new OkHttpClient();
Dispatcher dispatcher=new Dispatcher();
dispatcher.setMaxRequests(3);
client.setDispatcher(dispatcher);
// OkHttpClient client = new OkHttpClient();
// HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
// interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// client.interceptors().add(interceptor);
if(myService ==null){
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://chartapi.finance.yahoo.com/")
.addConverterFactory(JsonpGsonConverterFactory.create())
.client(client)
.build();
myService=retrofit.create(IStockChart.class);
return myService;
} else {
return myService;
}
}