retrofit

When should one use RxJava Observable and when simple Callback on Android?

走远了吗. 提交于 2019-12-17 06:19:31
问题 I'm working on networking for my app. So I decided to try out Square's Retrofit. I see that they support simple Callback @GET("/user/{id}/photo") void getUserPhoto(@Path("id") int id, Callback<Photo> cb); and RxJava's Observable @GET("/user/{id}/photo") Observable<Photo> getUserPhoto(@Path("id") int id); Both look pretty similar at first glance, but when it gets to implementation it gets interesting... While with simple callback implementation would look similar to this: api.getUserPhoto

How to use interceptor to add Headers in Retrofit 2.0?

心不动则不痛 提交于 2019-12-17 04:21:29
问题 Our team decide to adopt Retrofit 2.0 and I'm doing some initial research on it. I'm a newbie to this library. I'm wondering how to use interceptor to add customized headers via Retrofits 2.0 in our Android app. There are many tutorials about using interceptor to add headers in Retrofit 1.X, but since the APIs have changed a lot in the latest version, I'm not sure how to adapt those methods in the new version. Also, Retrofit hasn't update its new documentation yet. For example, in the

Retrofit 2: Get JSON from Response body

泪湿孤枕 提交于 2019-12-17 02:43:29
问题 I want to get string json from my api using retrofit 2, I have no problem when using retrofit 1 to get this json but using retrofit 2 returns null for me. This is what my json looks like {"id":1,"Username":"admin","Level":"Administrator"} This is my API @FormUrlEncoded @POST("/api/level") Call<ResponseBody> checkLevel(@Field("id") int id); This is how my code looks like Retrofit retrofit = new Retrofit.Builder() .baseUrl(Config.BASE_URL) .addConverterFactory(GsonConverterFactory.create())

Retrofit 源码阅读

人走茶凉 提交于 2019-12-16 20:17:48
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1.开发中一个常见网络请求过程 url,参数---> request ---> 转化成Http协议 -->请求执行 ---> 返回结果--> 转化成response ---> response转化成我们的对象 只有头和尾是我们日常开发进行自定义的,中间的其他过程都是网络框架进行的。 2.Retrofit做的事情 1.url,参数Retrofit主要采用接口+注解方式 2.中间网络请求框架采用动态代理去使用某个框架 3.response转化成我们的对象采用adapter模式进行适配 3.Retrofit的实现过程 1)toRequest,url,参数Retrofit主要采用接口+注解方式 我们以一个简单的POST请求为例 @POST("ssoService/v1/logoutCTGT") Call<BaseResponseBean> logout(@Body LogoutRequest logoutRequest); @Documented @Target(METHOD) @Retention(RUNTIME) public @interface POST { String value() default ""; } @Documented @Target(PARAMETER) @Retention

RxJava 中的map与flatMap

帅比萌擦擦* 提交于 2019-12-14 16:59:18
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 、map和flatMap都是接受一个函数作为参数(Func1) 2、map函数只有一个参数,参数一般是Func1,Func1的<I,O>I,O模版分别为输入和输出值的类型,实现Func1的call方法对I类型进行处理后返回O类型数据 3、flatMap函数也只有一个参数,也是Func1,Func1的<I,O>I,O模版分别为输入和输出值的类型,实现Func1的call方法对I类型进行处理后返回O类型数据,不过这里O为Observable类型 map实例 这里map里面直接对参数处理 flatMap实例: 这里用 Observable.just(s)在里面对参数进行处理并返回一个Observable, 上面两个例子效果是一样的,注意的就是参数,返回值不一样,我们再来看几个: 这里在map里面返回了一个Observable,注意subscribe的参数也要变成了Observable<String>,否则会有异常<这么写主要是当时不太明白map和flatMap的区别,想看一下到底有什么区别> Map一般用于对原始的参数进行加工处理,返回值还是基本的类型,可以在subscribe中使用(适用)的类型。 flatMap一般用于输出一个Observable

How to Access Child Item In JSON Array

淺唱寂寞╮ 提交于 2019-12-14 04:13:43
问题 I am querying FlickR based on certain search terms, and the response is a JSON Array. Here is the root level along with the first two results: { photos: { page: 1, pages: 4222, perpage: 100, total: "422175", photo: [ { id: "28571356563", owner: "8372889@N03",secret: "c4ca6c4364", server: "8050", farm: 9, title: "95040021.jpg", ispublic: 1, isfriend: 0, isfamily: 0, url_m: "https://farm9.staticflickr.com/8050/28571356563_c4ca6c4364.jpg", height_m: "332", width_m: "500" }, { id: "28571342883",

Retrofit and TypedInput

自闭症网瘾萝莉.ら 提交于 2019-12-14 04:09:57
问题 From the documentation, The TypedInput and TypedOutput classes exist only to bind a mime type to data. When we have to use the TypedInput in the Request. ?? Why we need this mapping while making the Request 回答1: If you have consistent MIME type for all requests then you shouldn't bother about the TypedInput or TypedOutput . Let's say if you have JSON in and out for your request you should not consider using these. But if you have mixed requests, let's say byte stream, JSON and XML you should

Deserialzing JSON with multiple types for a property

佐手、 提交于 2019-12-14 03:57:03
问题 I am working with a 3rd party API that returns three different types for the same JSON property, depending one how many nested objects it contains. I'm trying to figure out the best way to handle deserializing these objects using Jackson (preferably with Retrofit). A simplified example: when retrieving a Customer record from this API, the response might be any one of: Customer has multiple phone numbers; returns an array of PhoneObjects { "Phones": { "PhoneObject":[ {"number":"800 555 6666",

RxJavaCallAdapterFactory cannot be converted to Factory

旧街凉风 提交于 2019-12-14 03:43:45
问题 I'm trying to use Retrofit 2 and RxJava following the guide in this https://inthecheesefactory.com/blog/retrofit-2.0/en In section "RxJava Integration with CallAdapter " explains how use RxJava with retrofit Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://api.nuuneoi.com/base/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); However during compilation there is the following error: Error:(63, 71) error:

how to use Retrofit in Android? [closed]

南楼画角 提交于 2019-12-14 03:32:51
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I am having a hard time in understanding the Retrofit for my App. I want to use retrofit in it but I am not understanding about it much. Everthing is very confusing in it. Can any one help me to learn it completely I dont know how to start and from where to start The links I have