Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using retrofit behind a proxy

Tags:

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?

like image 452
Rudig Avatar asked Aug 17 '15 14:08

Rudig


People also ask

How retrofit works under the hood?

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.

What are the advantages of retrofit?

Advantages of retrofitIt supports request cancellation. It supports post requests and multipart uploads. It supports both synchronous and asynchronous network requests. Supports dynamic URLs.

What is retrofit used for?

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.

What is retrofit used for in Android?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.


2 Answers

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(); 
like image 142
fanky10 Avatar answered Sep 23 '22 21:09

fanky10


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

like image 23
Nikola Despotoski Avatar answered Sep 19 '22 21:09

Nikola Despotoski