retrofit

带你一步步剖析Retrofit 源码解析:一款基于 OkHttp 实现的网络请求框架

寵の児 提交于 2020-01-07 23:36:20
OkHttp与Retrofit 的区别与联系是怎样的? 参考答案: OkHttp和Retrofit都是目前流行网络开源框架 封装不同: Retrofit封装了具体的请求,线程切换以及数据转换。 retrofit通过使用代理,外观,策略模式对okhttp进行了封装 OkHttp 是基于Http协议封装的一套请求客户端 职责不同: Retrofit主要负责应用层面的封装,面向开发者,方便使用,比如请求参数,响应数据的处理,错误处理等等。 OkHttp主要负责socket部分的优化与封装,比如网络访问,多路复用,buffer缓存,数据压缩等等。 ( 顺手留下GitHub链接,需要获取相关面试等内容的可以自己去找 ) https://github.com/xiangjiana/Android-MS (VX:mm14525201314) Retrofit 可以说和 OkHttp 是亲兄弟了,它们都是由 Square 公司推出的网络请求库,并且 Retrofit 实际上是基于 OkHttp 实现的,它在 OkHttp 现有功能的基础上进行了封装,支持通过注解进行网络请求参数的配置,同时对数据返回后的解析、序列化进行了统一的包装,甚至在近期引入了对协程对支持。 今天就让我们一起来看看 Retrofit 是如何在 OkHttp 这样一个已经固定的框架的基础上,优雅的进行封装并拓展功能的。 基本使用

深度详解Retrofit2使用(一)基础入门

牧云@^-^@ 提交于 2020-01-07 08:42:35
前言 何为Retrofit? 借用官网的原话, Type-safe HTTP client for Android and Java by Square, Inc. 适用于Android 和 Java 的类型安全的HTTP客户端,由Square提供的。(敲黑板) 由此我们可以得知,Retrofit是一种HTTP客户端框架,使用它,我们可以完成有关HTTP的工作! (心中是不是会想到,Okhttp也是网络框架,到底哪一个好用而不贵呢?带着这个疑问,开始本篇文章!) 目前Retrofit最新版本是2.3.0,可以看看Retrofit的官网和Retrofit的github!本文中使用的Retrofit是Retrofit2.3.0版本。 一. Retrofit 入门 1.1 使用Retrofit前 ,需要先引入Retrofit 库。 1. 在Android中使用Retrofit ,只需要在build.gradle文件中添加以下代码, compile 'com.squareup.retrofit2:retrofit:2.3.0' 然后,同步就可以了!就是这么简单! 2. 在Java中使用Retrofit ,如果没有使用Maven或者其他项目管理工具的话,那么就需要我们手动将jar 包下载下来,而且需要下载好几个jar。下载完成后,加入到项目的build path

How to retrieve web service values through Retrofit to text view?

微笑、不失礼 提交于 2020-01-07 08:07:13
问题 I'm developing an app and in first activity it has card view layout. I'm retrieving data from a webservice and relevant data are showed in card view. It's working well. Now when a user clicks a particular card view I need to go for another activity. I'm getting relevant ID for that card view and passing it to the second activity. In second activity I need to show the content according to that unique Id. But I'm not getting any thing. This is what I tried. Pojo class public class PromoDetails

Android Retrofit - Wait for response before executing other code?

孤者浪人 提交于 2020-01-07 03:55:16
问题 I'm using Retrofit to return a response from an api. The data from that response is to be supplied to a recyclerview. I get a Null pointer exception on the data object when passing it to the recycler view. This is because, the app does not wait for the response to be returned before setting up the recyclerview. Is there anyway to have the app wait for the response before proceeding? I've tried making a synchronous retrofit request but I still get the same issue. I've tried calling

retrofit returning valid json but pojo is empty

爷,独闯天下 提交于 2020-01-06 19:38:19
问题 I am using retrofit to get a Json object from a server and I can get the Json (I see it completely and correct on the Log) but when I check the Object returned on the Callback it is empty which is weird. I was having this problem returning an array of JSON Objects but I already tried with a single object and the same thing happens. Here's the JSON: { "id" : "20176", "name" : "Dave Attwood", "dob" : "05/04/1987", "position" : "Lock" } Here is the the Player class: public class Player {

RxAndroid response of one call to make another request

别来无恙 提交于 2020-01-06 08:22:12
问题 I'm new to RxAndroid and trying to chain responses. I'm using this github API to retrieve data. Along with each issue there are comments link and events link associated with it which I want to fetch and update existing object with list of comments and events to form something like this. [ issue: { comments: [ { . . }, { . . } ] events : [ { . . }, { . . } ] ] ] I could retrieve initial response with following code GitHubService gitHubService = ServiceFactory.createServiceFrom(GitHubService

RxAndroid response of one call to make another request

拟墨画扇 提交于 2020-01-06 08:21:12
问题 I'm new to RxAndroid and trying to chain responses. I'm using this github API to retrieve data. Along with each issue there are comments link and events link associated with it which I want to fetch and update existing object with list of comments and events to form something like this. [ issue: { comments: [ { . . }, { . . } ] events : [ { . . }, { . . } ] ] ] I could retrieve initial response with following code GitHubService gitHubService = ServiceFactory.createServiceFrom(GitHubService

Parsing JSON array that contain objects with different attributes with Retrofit

瘦欲@ 提交于 2020-01-06 07:12:09
问题 how can I parse JSON array that contains JSON objects without names and each object have his own attributes in Android with Retrofit2. Json is something like this: [ { "username":"alexruskovski", "age":27, "active":true }, { "languages":"Java", "occupation":"Programming", "phone_num":"123456789", "email":"asdf@qwe.com" } ] And I have my POJO's like this: user: public class User{ String username; int age; boolean active; } and here is the data object: public class Data{ String languages,

Android Retrofit - GoogleBooksAPI - JSON return issue

耗尽温柔 提交于 2020-01-06 06:58:45
问题 I'm trying to make my android app to return book info using the google book API. Here's the url I'm pinging I'm using retrofit to grab the info and bring it back to me however it's coming back like this and I'm not sure how to resolve it: D/MainActivity2: onResponse: Success onResponse: success body: models.Book2@ae8dded onResponse: success response: Response{protocol=h2, code=200, message=, url=https://www.googleapis.com/books/v1/volumes?q=enders+game} onResponse: success raw: Response

Android Retrofit - GoogleBooksAPI - JSON return issue

╄→尐↘猪︶ㄣ 提交于 2020-01-06 06:58:18
问题 I'm trying to make my android app to return book info using the google book API. Here's the url I'm pinging I'm using retrofit to grab the info and bring it back to me however it's coming back like this and I'm not sure how to resolve it: D/MainActivity2: onResponse: Success onResponse: success body: models.Book2@ae8dded onResponse: success response: Response{protocol=h2, code=200, message=, url=https://www.googleapis.com/books/v1/volumes?q=enders+game} onResponse: success raw: Response