use of okhttp3.OkHttpClient in project okhttp by square.
the class URLConnectionTest method disconnectDuringConnect_cookieJar.
@Test
public void disconnectDuringConnect_cookieJar() throws Exception {
final AtomicReference<HttpURLConnection> connectionHolder = new AtomicReference<>();
class DisconnectingCookieJar implements CookieJar {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
connectionHolder.get().disconnect();
return Collections.emptyList();
}
}
OkHttpClient client = new okhttp3.OkHttpClient.Builder().cookieJar(new DisconnectingCookieJar()).build();
URL url = server.url("path that should never be accessed").url();
HttpURLConnection connection = new OkHttpURLConnection(url, client);
connectionHolder.set(connection);
try {
connection.getInputStream();
fail("Connection should not be established");
} catch (IOException expected) {
assertEquals("Canceled", expected.getMessage());
} finally {
connection.disconnect();
}
}
use of okhttp3.OkHttpClient in project okhttp by square.
the class CallTest method asyncCallEngineInitialized.
/** https://github.com/square/okhttp/issues/1801 */
@Test
public void asyncCallEngineInitialized() throws Exception {
OkHttpClient c = defaultClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
throw new IOException();
}
}).build();
Request request = new Request.Builder().url(server.url("/")).build();
c.newCall(request).enqueue(callback);
RecordedResponse response = callback.await(request.url());
assertEquals(request, response.request);
}
use of okhttp3.OkHttpClient in project okhttp by square.
the class OkUrlFactory method open.
HttpURLConnection open(URL url, Proxy proxy) {
String protocol = url.getProtocol();
OkHttpClient copy = client.newBuilder().proxy(proxy).build();
if (protocol.equals("http"))
return new OkHttpURLConnection(url, copy, urlFilter);
if (protocol.equals("https"))
return new OkHttpsURLConnection(url, copy, urlFilter);
throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
use of okhttp3.OkHttpClient in project GitTest by xiaoxige.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testView = (TestView) findViewById(R.id.testView);
testView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivityPermissionsDispatcher.showCameraWithPermissionCheck(MainActivity.this);
}
});
try {
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
String appMV = appInfo.metaData.getString("MTA_CHANNEL");
Toast.makeText(MainActivity.this, "appMV = " + appMV, Toast.LENGTH_SHORT).show();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
simpleDraweeView = (SimpleDraweeView) findViewById(R.id.simpleDraweeView);
testView.setProgress(100, false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
testView.setProgress(3, true);
}
}, 2000);
GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy();
RoundingParams roundingParams = new RoundingParams();
roundingParams.setBorder(Color.RED, 10);
roundingParams.setRoundAsCircle(true);
hierarchy.setRoundingParams(roundingParams);
simpleDraweeView.setHierarchy(hierarchy);
ControllerListener listener = new BaseControllerListener() {
@Override
public void onFinalImageSet(String id, @Nullable Object imageInfo, @Nullable Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
}
};
DraweeController controller = Fresco.newDraweeControllerBuilder().setUri(new Uri.Builder().scheme(UriUtil.LOCAL_RESOURCE_SCHEME).path(String.valueOf(R.mipmap.ic_launcher)).build()).setOldController(simpleDraweeView.getController()).setControllerListener(listener).build();
simpleDraweeView.setController(controller);
/**
* 网络相关(retrofit+rxjava+rxlife+stetho)
*/
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5000, TimeUnit.SECONDS).readTimeout(5000, TimeUnit.SECONDS).writeTimeout(5000, TimeUnit.SECONDS).addNetworkInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Headers headers = request.headers();
Headers build = headers.newBuilder().add("version", "1.0").add("token", "xiaoxige").build();
request = request.newBuilder().headers(build).build();
Log.e("TAG", "url = " + request.url().uri().toString());
String method = request.method();
if (method.equals("GET") || method.equals("DELETE")) {
HttpUrl url = request.url();
HttpUrl httpUrl = url.newBuilder().addQueryParameter("xiaoxige", "one").addQueryParameter("zhuxiaoan", "two").build();
request = request.newBuilder().url(httpUrl).build();
} else {
RequestBody body = request.body();
if (body != null) {
Buffer buffer = new Buffer();
body.writeTo(buffer);
String readUtf8 = buffer.readUtf8();
// 可能需要对body进行加密
// TODO: 2017/11/3
RequestBody requestBody = RequestBody.create(body.contentType(), readUtf8);
request = request.newBuilder().method(method, requestBody).build();
}
}
return chain.proceed(request);
}
}).addNetworkInterceptor(new StethoInterceptor()).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.baidu.com").addConverterFactory(ScalarsConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).client(client).build();
final NetApi api = retrofit.create(NetApi.class);
Flowable flowable = Flowable.create(new FlowableOnSubscribe<String>() {
@Override
public void subscribe(@NonNull FlowableEmitter<String> flowableEmitter) throws Exception {
String response = MainActivity.execute(api.getBaiduWeb());
if (TextUtils.isEmpty(response)) {
flowableEmitter.onError(new Exception());
return;
}
flowableEmitter.onNext(response);
flowableEmitter.onComplete();
}
}, BackpressureStrategy.LATEST).compose(bindUntilEvent(ActivityEvent.DESTROY)).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
flowable.subscribe(new XXGSubscriber<String>() {
@Override
public void xxgNext(String o) {
super.xxgNext(o);
Log.e("TAG", "o = " + o);
}
@Override
public void xxgError(Throwable t) {
super.xxgError(t);
Log.e("TAG", "t = " + t.getMessage());
}
@Override
public void xxgComplete() {
super.xxgComplete();
Log.e("TAG", "Complete");
}
});
}
use of okhttp3.OkHttpClient in project sonarlint-core by SonarSource.
the class OkHttpClientBuilderTest method build_default_instance_of_OkHttpClient.
@Test
public void build_default_instance_of_OkHttpClient() {
OkHttpClient okHttpClient = underTest.build();
assertThat(okHttpClient.proxy()).isNull();
assertThat(okHttpClient.networkInterceptors()).hasSize(1);
assertThat(okHttpClient.sslSocketFactory()).isNotNull();
}
Aggregations