I am trying to call a Restful service with Retrofit from behind a proxy. Is there anyway to to set proxy settings on Retrofit in the code?
Under the hood, it uses the OkHttp library as its HTTP client ( square.github.io/okhttp ). Retrofit helps you build an HTTP gateway class. You write an interface with annotated instance methods, and Retrofit creates the implementation.
Advantages of retrofitIt supports request cancellation. It supports post requests and multipart uploads. It supports both synchronous and asynchronous network requests. Supports dynamic URLs.
Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.
Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.
Transforming Nikola's words into code:
java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); OkHttpClient client = new OkHttpClient.Builder().proxy(proxy).build(); Retrofit.Builder builder = new Retrofit.Builder().client(client); Retrofit retrofit = builder.build();
Retrofit does not have options for setting any network related settings. You need to set proxy to your Retrofit http client.
Set Proxy, to your OkHttpClient
using setProxy(proxy)
method
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