why use Retrofit when we have OkHttp

JavaAndroidRetrofitOkhttp

Java Problem Overview


with OkHttp we can make HTTP request then get response from server

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
  .url(url)
  .build();
Response response = client.newCall(request).execute();

then with Gson lib convert response to object we need.

this is from Square/OkHttp doc:

> Its request/response API is designed with fluent builders and > immutability. It supports both synchronous blocking calls and async > calls with callbacks

I read from [stackOverFlow][1]

> Retrofit uses OkHTTP automatically if available

.

So my question is what is exactly Retrofit for?

what Retrofit can do that OkHttp can not?!

I think OkHttp and Gson solve request API problem, so what problem Retrofit solve for us?

[1]: https://stackoverflow.com/questions/16902716/comparison-of-android-networking-libraries-okhttp-retrofit-volley "stackOverFlow"

Java Solutions


Solution 1 - Java

> with OkHttp we can make HTTP request then get response from server... then with Gson lib convert response to object we need

Note that in your code snippet, you skipped two notable steps: generating the URL and actually parsing the JSON using Gson.

> So my question is what is exactly Retrofit for?

It is for generating the URL (using type-aware generated code tied to your specific REST API) and actually parsing the JSON using Gson. In other words, it does what you skipped in your code snippet.

Also, for certain types of REST operations (e.g., POST), it helps a bit in assembling what to submit (e.g., generating the encoded form).

By definition, you do not need to use Retrofit. Retrofit is computer code, written by computer programmers. Somebody else could write code to do what Retrofit does.

> why Retrofit use OkHttp

Retrofit needs to perform HTTP operations. It uses OkHttp where available, for all that OkHttp provides: HTTP/2 and SPDY support, pluggable interceptors, etc.

Solution 2 - Java

You should use retrofit if you are trying to map your server API inside your application (type-safing). Retrofit is just an API adapter wrapped over okHTTP.

If you want to type safe and modularise the interaction code with your API, use retrofit. Apart from that, the underlying performance, request defaults, etc of okHTTP and Retrofit are the same.

Also I would recommend listening to this podcast from Jesse Wilson (developer of major android HTTP clients), where he talks in-depth of the history of development of Apache HTTP client, HTTPURLConnection, okHTTP and Retrofit.

Solution 3 - Java

Retrofit vs. OkHttp The reason is simple: OkHttp is a pure HTTP/SPDY client responsible for any low-level network operation, caching, request and response manipulation, and many more. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. Retrofit 2 is strongly coupled with OkHttp and makes intensive use of it.

OkHttp Functions: Connection pooling, gzipping, caching, recovers from network problems, sync, and async calls, redirects, retries … and so on.

Retrofit Functions: URL manipulation, requesting, loading, caching, threading, synchronization... It allows sync and async calls.

Solution 4 - Java

Retrofit is basically architecture above the OKHTTP, it internally uses OkHttp to make any request , earlier in jave if we want to make any request we have HTTPUrl connection or HTTPS Url connect know retrofit okHttp handles everything ( it divides into packages it marks headers )for us if we need to send some information .

Retrofit is a rest client that is based on restful principle .

->OkHttp is a an HTTP client, which supports HTTP/2 and SPDY.

->Retrofit is a type safe HTTP client for android and java

->OkHttp depends on Okio.

->Retrofit depends on OkHttP,

so Retrofit is basically a wrapper on OKHTTP ,it uses when necessity and can easily manage connect timeout and read timeout just using it’s methods and also adds Interceptor.

Hope I answered !!! happy coding!!!!

for more information refer https://square.github.io/retrofit

Solution 5 - Java

OkHttp: An open source HTTP client. HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth; Retrofit: A type-safe HTTP client for Android and Java. Retrofit turns your HTTP API into a Java interface.

OkHttp and Retrofit can be primarily classified as "API" tools.

Some of the features offered by OkHttp are: • HTTP/2 support allows all requests to the same host to share a socket. • Connection pooling reduces request latency (if HTTP/2 isn’t available). • Transparent GZIP shrinks download sizes.

On the other hand, Retrofit provides the following key features: • URL parameter replacement and query parameter support • Object conversion to request body (e.g., JSON, protocol buffers) • Multipart request body and file upload

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMehrdad FarajiView Question on Stackoverflow
Solution 1 - JavaCommonsWareView Answer on Stackoverflow
Solution 2 - JavageekoraulView Answer on Stackoverflow
Solution 3 - JavaWubbalubbadubdubView Answer on Stackoverflow
Solution 4 - JavaMouliView Answer on Stackoverflow
Solution 5 - JavaRajesh ParmarView Answer on Stackoverflow