use of okhttp3.OkHttpClient.Builder in project Gradle-demo by Arisono.
the class RetrofitImpl method initClient.
@Override
public void initClient() {
// 本类保证初始化一次,减少系统开销
Builder okBuilder = new OkHttpClient.Builder().connectTimeout(mbuilder.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(mbuilder.getReadTimeout(), TimeUnit.SECONDS).writeTimeout(mbuilder.getWriteTimeout(), TimeUnit.SECONDS).sslSocketFactory(OkhttpUtils.createSSLSocketFactory(), // 信任所有证书
new TrustAllCerts()).hostnameVerifier(new TrustAllHostnameVerifier());
LogInterceptor logInterceptor = new LogInterceptor();
logInterceptor.setBuilder(mbuilder);
okBuilder.addInterceptor(logInterceptor);
if (mbuilder.getCacheFileSize() != 0) {
okBuilder.cache(new Cache(mbuilder.getCacheFile(), mbuilder.getCacheFileSize()));
okBuilder.addInterceptor(new CacheInterceptor(String.valueOf(mbuilder.getCacheTime()), mbuilder.getCacheType()));
}
//后期缓存策略改进
switch(mbuilder.getCacheType()) {
case CacheType.ONLY_NETWORK:
OkhttpUtils.println("CacheType.ONLY_NETWORK");
break;
case CacheType.ONLY_CACHED:
OkhttpUtils.println("CacheType.ONLY_CACHED");
break;
case CacheType.CACHED_ELSE_NETWORK:
break;
case CacheType.NETWORK_ELSE_CACHED:
break;
default:
break;
}
OkHttpClient client = okBuilder.build();
retrofit = new Retrofit.Builder().client(client).baseUrl(mbuilder.getBaseUrl()).addConverterFactory(StringConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
}
use of okhttp3.OkHttpClient.Builder in project Gradle-demo by Arisono.
the class RetrofitUtils method uploads.
public void uploads(Subscriber<Object> s, String url, Map<String, Object> params) {
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
//追加参数
for (String key : params.keySet()) {
Object object = params.get(key);
if (!(object instanceof File)) {
builder.addFormDataPart(key, object.toString());
} else {
File file = (File) object;
//其中参数“file”和服务器接收的参数 一一对应,保证多文件上传唯一key不变
builder.addFormDataPart("file", file.getName(), RequestBody.create(null, file));
}
}
//创建RequestBody
RequestBody body = builder.build();
Observable<Object> o = paramService.uploads(url, body);
toSubscribe(o, s);
}
use of okhttp3.OkHttpClient.Builder in project AntennaPod by AntennaPod.
the class GpodnetService method uploadSubscriptions.
/**
* Uploads the subscriptions of a specific device.
* <p/>
* This method requires authentication.
*
* @param username The username. Must be the same user as the one which is
* currently logged in.
* @param deviceId The ID of the device whose subscriptions should be updated.
* @param subscriptions A list of feed URLs containing all subscriptions of the
* device.
* @throws IllegalArgumentException If username, deviceId or subscriptions is null.
* @throws GpodnetServiceAuthenticationException If there is an authentication error.
*/
public void uploadSubscriptions(@NonNull String username, @NonNull String deviceId, @NonNull List<String> subscriptions) throws GpodnetServiceException {
try {
URL url = new URI(BASE_SCHEME, BASE_HOST, String.format("/subscriptions/%s/%s.txt", username, deviceId), null).toURL();
StringBuilder builder = new StringBuilder();
for (String s : subscriptions) {
builder.append(s);
builder.append("\n");
}
RequestBody body = RequestBody.create(TEXT, builder.toString());
Request.Builder request = new Request.Builder().put(body).url(url);
executeRequest(request);
} catch (MalformedURLException | URISyntaxException e) {
e.printStackTrace();
throw new GpodnetServiceException(e);
}
}
use of okhttp3.OkHttpClient.Builder in project amhttp by Eddieyuan123.
the class RequestBodyFactoryImpl method buildRequestBody.
@Override
public RequestBody buildRequestBody(File file, String fileName, HashMap<String, String> params) {
MultipartBody.Builder builder = new MultipartBody.Builder();
if (params == null) {
throw new NullPointerException("requestBodyFactory build params is null");
} else {
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
try {
if (params.size() > 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.addFormDataPart(entry.getKey(), entry.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("file", fileName, fileBody);
}
return builder.build();
}
use of okhttp3.OkHttpClient.Builder in project muzei by romannurik.
the class OkHttpClientFactory method enableTls12.
/**
* Enable TLS on the OKHttp builder by setting a custom SocketFactory
*/
private static OkHttpClient.Builder enableTls12(OkHttpClient.Builder client) {
Log.i(TAG, "Enabling HTTPS compatibility mode");
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
client.sslSocketFactory(new TLSSocketFactory(), trustManager);
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1).build();
List<ConnectionSpec> specs = new ArrayList<>();
specs.add(cs);
specs.add(ConnectionSpec.COMPATIBLE_TLS);
specs.add(ConnectionSpec.CLEARTEXT);
client.connectionSpecs(specs);
} catch (Exception exc) {
Log.e(TAG, "Error while setting TLS", exc);
}
return client;
}
Aggregations