use of okhttp3.OkHttpClient in project twitter4j by yusuke.
the class Http2ClientTest method testHttp2.
public void testHttp2() throws Exception {
AlternativeHttpClientImpl.sPreferSpdy = false;
AlternativeHttpClientImpl.sPreferHttp2 = true;
AlternativeHttpClientImpl http = callOembed();
// check HTTP/2.0
Field f = http.getClass().getDeclaredField("okHttpClient");
f.setAccessible(true);
OkHttpClient client = (OkHttpClient) f.get(http);
assertNotNull("ensure that OkHttpClient is used", client);
ConnectionPool p = client.connectionPool();
assertEquals(1, p.connectionCount());
assertEquals(Protocol.HTTP_2, http.getLastRequestProtocol());
}
use of okhttp3.OkHttpClient in project twitter4j by yusuke.
the class Http2ClientTest method testNoSpdy.
public void testNoSpdy() throws Exception {
AlternativeHttpClientImpl.sPreferSpdy = false;
AlternativeHttpClientImpl.sPreferHttp2 = false;
AlternativeHttpClientImpl http = callOembed();
// check not SPDY
Field f = http.getClass().getDeclaredField("okHttpClient");
f.setAccessible(true);
OkHttpClient client = (OkHttpClient) f.get(http);
ConnectionPool p = client.connectionPool();
assertEquals(1, p.connectionCount());
assertEquals(Protocol.HTTP_1_1, http.getLastRequestProtocol());
}
use of okhttp3.OkHttpClient in project YourAppIdea by Michenux.
the class MongolabPlaceServiceFactory method create.
public static MongolabPlaceService create(Context context) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampDeserializer());
gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer());
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build();
String url = context.getString(R.string.aroundme_placeremoteprovider_url);
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).client(client).addConverterFactory(GsonConverterFactory.create(gsonBuilder.create())).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
return retrofit.create(MongolabPlaceService.class);
}
use of okhttp3.OkHttpClient in project OkHttp3 by MrZhousf.
the class DownUpLoadHelper method downloadFile.
/**
* 文件下载
*/
void downloadFile(final OkHttpHelper helper) {
try {
final HttpInfo info = httpInfo;
final DownloadFileInfo fileInfo = helper.getDownloadFileInfo();
String url = fileInfo.getUrl();
if (TextUtils.isEmpty(url)) {
showLog("下载文件失败:文件下载地址不能为空!");
return;
}
info.setUrl(url);
ProgressCallback progressCallback = fileInfo.getProgressCallback();
//获取文件断点
long completedSize = fetchCompletedSize(fileInfo);
fileInfo.setCompletedSize(completedSize);
//添加下载任务
if (null == downloadTaskMap)
downloadTaskMap = new ConcurrentHashMap<>();
if (downloadTaskMap.containsKey(fileInfo.getSaveFileNameEncrypt())) {
showLog(fileInfo.getSaveFileName() + " 已在下载任务中");
return;
}
downloadTaskMap.put(fileInfo.getSaveFileNameEncrypt(), fileInfo.getSaveFileNameEncrypt());
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), fileInfo, timeStamp, requestTag)).build();
}
};
//采用新的OkHttpClient处理多线程干扰回调进度问题
OkHttpClient httpClient = helper.getClientBuilder().addInterceptor(interceptor).build();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(url).header("RANGE", "bytes=" + completedSize + "-");
helper.getHttpHelper().addHeadsToRequest(info, requestBuilder);
Request request = requestBuilder.build();
helper.setRequest(request);
helper.setHttpClient(httpClient);
helper.getHttpHelper().responseCallback(helper.doRequestSync(), progressCallback, OkMainHandler.RESPONSE_DOWNLOAD_CALLBACK, requestTag);
//删除下载任务
if (null != downloadTaskMap) {
downloadTaskMap.remove(fileInfo.getSaveFileNameEncrypt());
}
} catch (Exception e) {
showLog("下载文件失败:" + e.getMessage());
}
}
use of okhttp3.OkHttpClient in project yyl_example by Relucent.
the class OkhttpTest2 method main.
public static void main(String[] args) throws IOException {
OkHttpClient client;
(client = //
new OkHttpClient.Builder().build()).newCall(//
new Request.Builder().url(//
"https://www.baidu.com/").header("Connection", //close | keep-alive
"close").get().build()).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.body().string());
}
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
});
//应用关闭时候需要关闭线程池
client.dispatcher().executorService().shutdown();
}
Aggregations