use of okhttp3.OkHttpClient 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 in project Gradle-demo by Arisono.
the class OkhttpUtilsMain method okBasicRequest.
public static void okBasicRequest() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://www.baidu.com").build();
try {
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
}
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of okhttp3.OkHttpClient in project Gradle-demo by Arisono.
the class OkhttpUtilsMain method sendFormParams.
/**
* 表单提交
*/
public static void sendFormParams() {
Map<String, Object> publicMap = RSAEncodeToken();
String miwen = (String) publicMap.get("miwen");
String public_key = (String) publicMap.get("public_key");
RequestBody formBody = new FormBody.Builder().add("username", "123").add("password", "df13edafsdddsads").add("publicKey", public_key).add("miwen", miwen).build();
//postBody 接收
String json_1 = "{}";
@SuppressWarnings("unused") String bytes = "username=123&password=df13edafsdddsads";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost:8080/spring-mvc-showcase/http/getHeaders").header("cookie", "JSESSIONID=EB36DE5E50E342D86C55DAE0CDDD4F6D").addHeader("content-type", "text/html;charset:utf-8").post(RequestBody.create(JSONTYPE, json_1)).post(formBody).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String json = response.body().string();
System.out.println(json);
} else {
System.out.println(JSON.toJSONString(response.code()));
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("******************************************************");
}
use of okhttp3.OkHttpClient in project BookReader by JustWayward.
the class BookApiModule method provideOkHttpClient.
@Provides
public OkHttpClient provideOkHttpClient() {
LoggingInterceptor logging = new LoggingInterceptor(new Logger());
logging.setLevel(LoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).connectTimeout(20 * 1000, TimeUnit.MILLISECONDS).readTimeout(20 * 1000, TimeUnit.MILLISECONDS).retryOnConnectionFailure(// 失败重发
true).addInterceptor(new HeaderInterceptor()).addInterceptor(logging);
return builder.build();
}
use of okhttp3.OkHttpClient in project jianshi by wingjay.
the class AppModule method provideOkHttpClient.
@Provides
@Singleton
OkHttpClient provideOkHttpClient(GlobalRequestInterceptor globalRequestInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS)).connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(globalRequestInterceptor).retryOnConnectionFailure(false);
if (BuildConfig.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
builder.addInterceptor(httpLoggingInterceptor);
return builder.build();
}
Aggregations