[Java][okhttp-gson] Use builder api of OkHttpClient to avoid UnsupportedOperationEx (#5177)

* 3432 Use builder api of OkHttpClient to avoid UnsupportedOperationEx

The reason for the UnsupportedOperationException was an invalid access to the interceptors list of OkHttpClient. That list is unmodifiable as stated by the JavaDoc. Instead accessing the list directly, the interceptors should always be passed when building the client by the builder api.

* 3432 Use builder api of OkHttpClient to avoid UnsupportedOperationEx

The reason for the UnsupportedOperationException was an invalid access to the interceptors list of OkHttpClient. That list is unmodifiable as stated by the JavaDoc. Instead accessing the list directly, the interceptors should always be passed when building the client by the builder api.

* 3432 Follow-up fix to make samples compile again

* 3432 Updated sample implementation for okHttp-Gson-ParcelableModel
This commit is contained in:
Timo Rohrberg 2020-02-13 14:55:10 +01:00 committed by GitHub
parent bca130cd1f
commit f5265853ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 12 deletions

View File

@ -90,6 +90,7 @@ public class ApiClient {
*/
public ApiClient() {
init();
initHttpClient();
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
@ -129,7 +130,7 @@ public class ApiClient {
"{{name}}",
retryingOAuth
);
httpClient.interceptors().add(retryingOAuth);
initHttpClient(Collections.<Interceptor>singletonList(retryingOAuth));
{{/hasOAuthMethods}}
// Prevent the authentications from being modified.
@ -139,16 +140,25 @@ public class ApiClient {
{{/-first}}
{{/oauthMethods}}
{{/hasOAuthMethods}}
private void init() {
private void initHttpClient() {
initHttpClient(Collections.<Interceptor>emptyList());
}
private void initHttpClient(List<Interceptor> interceptors) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addNetworkInterceptor(getProgressInterceptor());
httpClient = builder.build();
for (Interceptor interceptor: interceptors) {
builder.addInterceptor(interceptor);
}
{{#useGzipFeature}}
// Enable gzip request compression
httpClient.interceptors().add(new GzipRequestInterceptor());
// Enable gzip request compression
builder.addInterceptor(new GzipRequestInterceptor());
{{/useGzipFeature}}
httpClient = builder.build();
}
private void init() {
verifyingSsl = true;
json = new JSON();

View File

@ -85,6 +85,7 @@ public class ApiClient {
*/
public ApiClient() {
init();
initHttpClient();
// Setup authentications (key: authentication name, value: authentication).
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
@ -120,18 +121,27 @@ public class ApiClient {
"petstore_auth",
retryingOAuth
);
httpClient.interceptors().add(retryingOAuth);
initHttpClient(Collections.<Interceptor>singletonList(retryingOAuth));
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
private void init() {
private void initHttpClient() {
initHttpClient(Collections.<Interceptor>emptyList());
}
private void initHttpClient(List<Interceptor> interceptors) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addNetworkInterceptor(getProgressInterceptor());
for (Interceptor interceptor: interceptors) {
builder.addInterceptor(interceptor);
}
httpClient = builder.build();
}
private void init() {
verifyingSsl = true;
json = new JSON();

View File

@ -85,6 +85,7 @@ public class ApiClient {
*/
public ApiClient() {
init();
initHttpClient();
// Setup authentications (key: authentication name, value: authentication).
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
@ -120,18 +121,27 @@ public class ApiClient {
"petstore_auth",
retryingOAuth
);
httpClient.interceptors().add(retryingOAuth);
initHttpClient(Collections.<Interceptor>singletonList(retryingOAuth));
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
private void init() {
private void initHttpClient() {
initHttpClient(Collections.<Interceptor>emptyList());
}
private void initHttpClient(List<Interceptor> interceptors) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addNetworkInterceptor(getProgressInterceptor());
for (Interceptor interceptor: interceptors) {
builder.addInterceptor(interceptor);
}
httpClient = builder.build();
}
private void init() {
verifyingSsl = true;
json = new JSON();