I'm trying to connect okHttp with a web service but with different port, for example, 10000. My idea to stub the responses with a proxy during the unit tests. Neverthless, there is not enough documentation to make it with this library. As a matter of fact, my implementation is:
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(TIMEOUT_MS, TimeUnit.MILLISECONDS);
For example, with other libraries such as loopj, this feature is possible:
httpClient.setProxy("localhost", 10000);
I'm using this version: com.squareup.okhttp:okhttp:2.5.0
OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing connections and threads reduces latency and saves memory.
close() will release all resources held by the response. The connection pool will keep the connection open, but that'll get closed automatically after a timeout if it goes unused. You can also call response. close() which, according to the documentation, is equivalent to response.
OkHttpClient client = new OkHttpClient(); Request getRequest = new Request. Builder() . url("https://mytodoserver.com/todolist") . build(); client.
OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features, such as connection pooling (if HTTP/2 isn't available), transparent GZIP compression, and response caching, to avoid the network completely for repeated requests.
I'm trying to connect okHttp with a web service but with different port, for example, 10000
Put the port in the URL that you supply to Request.Builder
:
Request request = new Request.Builder()
.url("http://publicobject.com:10000/helloworld.txt")
.build();
My idea to stub the responses with a proxy during the unit tests.
Use a different URL for the tests than you do in production.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With