Java 当我们有 OkHttp 时为什么要使用 Retrofit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39183294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why use Retrofit when we have OkHttp
提问by Mehrdad Faraji
with OkHttp we can make HTTP request then get response from server
使用 OkHttp 我们可以发出 HTTP 请求,然后从服务器获得响应
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.
然后使用 Gson lib 将响应转换为我们需要的对象。
this is from Square/OkHttp doc:
这是来自 Square/OkHttp 文档:
Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks
它的请求/响应 API 设计有流畅的构建器和不变性。它支持同步阻塞调用和带有回调的异步调用
I read from stackOverFlow
我从stackOverFlow 中读取
Retrofit uses OkHTTP automatically if available
如果可用,Retrofit 会自动使用 OkHTTP
.
.
So my question is what is exactly Retrofit for?
所以我的问题是 Retrofit 到底是为了什么?
what Retrofit can do that OkHttp can not?!
什么 Retrofit 能做到而 OkHttp 不能?!
I think OkHttp and Gson solve request API problem, so what problem Retrofit solve for us?
我认为 OkHttp 和 Gson 解决了请求 API 的问题,那么 Retrofit 为我们解决了什么问题?
采纳答案by CommonsWare
with OkHttp we can make HTTP request then get response from server... then with Gson lib convert response to object we need
使用 OkHttp 我们可以发出 HTTP 请求,然后从服务器获取响应......然后使用 Gson lib 将响应转换为我们需要的对象
Note that in your code snippet, you skipped two notable steps: generating the URL and actually parsing the JSON using Gson.
请注意,在您的代码片段中,您跳过了两个值得注意的步骤:生成 URL 和使用 Gson 实际解析 JSON。
So my question is what is exactly Retrofit for?
所以我的问题是 Retrofit 到底是为了什么?
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.
它用于生成 URL(使用绑定到特定 REST API 的类型感知生成代码)并使用 Gson 实际解析 JSON。换句话说,它会执行您在代码片段中跳过的操作。
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).
此外,对于某些类型的 REST 操作(例如,POST),它有助于组装要提交的内容(例如,生成编码表单)。
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.
根据定义,您不需要使用 Retrofit。Retrofit 是计算机代码,由计算机程序员编写。其他人可以编写代码来完成 Retrofit 所做的事情。
why Retrofit use OkHttp
为什么 Retrofit 使用 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.
Retrofit 需要执行 HTTP 操作。它在可用的情况下使用 OkHttp,因为 OkHttp 提供的所有内容:HTTP/2 和 SPDY 支持、可插入拦截器等。
回答by geekoraul
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.
如果您尝试在应用程序中映射服务器 API(类型安全),则应该使用改造。Retrofit 只是一个封装在 okHTTP 上的 API 适配器。
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.
如果您想输入安全并模块化与您的 API 的交互代码,请使用改造。除此之外,okHTTP 和 Retrofit 的底层性能、请求默认值等是相同的。
Also I would recommend listening to thispodcast 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.
此外,我建议您收听Jesse Wilson(主要 android HTTP 客户端的开发者)的这个播客,他在那里深入讨论了 Apache HTTP 客户端、HTTPURLConnection、okHTTP 和 Retrofit 的开发历史。
回答by Malti Devnani
Retrofit is a REST Client for Android and Java by Square. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based Webservice. In Retrofit, you configure which converter is used for the data serialization. Typically for JSON, you use GSon, but you can add custom converters to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests. You should use retrofit over okhttp for its ease, for its features.
Retrofit 是 Square 的 Android 和 Java REST 客户端。它使得通过基于 REST 的 Web 服务检索和上传 JSON(或其他结构化数据)变得相对容易。在 Retrofit 中,您可以配置用于数据序列化的转换器。通常对于 JSON,您使用 GSon,但您可以添加自定义转换器来处理 XML 或其他协议。Retrofit 使用 OkHttp 库来处理 HTTP 请求。您应该对 okhttp 使用改造,因为它的功能很简单。
回答by Wubbalubbadubdub
Retrofit vs. OkHttpThe 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.
Retrofit vs. OkHttp原因很简单:OkHttp 是一个纯 HTTP/SPDY 客户端,负责任何底层网络操作、缓存、请求和响应操作等等。相比之下,Retrofit 是构建在 OkHttp 之上的高级 REST 抽象。Retrofit 2 与 OkHttp 强耦合并大量使用它。
OkHttp Functions:Connection pooling, gzipping, caching, recovers from network problems, sync, and async calls, redirects, retries … and so on.
OkHttp 功能:连接池、压缩、缓存、从网络问题中恢复、同步和异步调用、重定向、重试……等等。
Retrofit Functions:URL manipulation, requesting, loading, caching, threading, synchronization... It allows sync and async calls.
改造功能:URL 操作、请求、加载、缓存、线程、同步……它允许同步和异步调用。