retrofit

Simple custom mapping of JSON property to object property with Retrofit

五迷三道 提交于 2019-12-23 18:15:00
问题 What is the simplest way to define a custom mapping of a JSON property to a particular object property in RetroFit? Example JSON response for a set of "rewards": [ { "name" : "$5 Voucher", "description" : "Get $5 off your next purchase", "assets" : { "icon" : "icon.png" } } ] Reward class in my Android project: public class Reward { @SerializedName("name") private String name; @SerializedName("description") private String description; @SerializedName("icon") private String icon; } Because

Feign源码学习

独自空忆成欢 提交于 2019-12-23 17:53:36
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> feign介绍 Feign是一款java的Restful客户端组件,Feign使得 Java HTTP 客户端编写更方便。Feign 灵感来源于Retrofit, JAXRS-2.0和WebSocket。feign在github上有近3K个star,是一款相当优秀的开源组件,虽然相比Retrofit的近30K个star,逊色了太多,但是spring cloud集成了feign,使得feign在java生态中比Retrofit使用的更加广泛。 feign的基本原理是在接口方法上加注解,定义rest请求,构造出接口的动态代理对象,然后通过调用接口方法就可以发送http请求,并且自动解析http响应为方法返回值,极大的简化了客户端调用rest api的代码。官网的示例如下: interface GitHub { @RequestLine("GET /repos/{owner}/{repo}/contributors") List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); } static class Contributor { String login; int contributions;

Retrofit 404 not found web api

你离开我真会死。 提交于 2019-12-23 17:00:36
问题 I have a web api and an application. So I want to a Register app but I have a problem. I use the azure. There is my registerapi (interface) @FormUrlEncoded @POST("/application/json") public void insertUser( @Field("Username") String Username, @Field("Password") String Password, @Field("Email") String Email, Callback<Response> callback); and my mainactivty.java page public class MainActivity extends AppCompatActivity { private EditText editTextUsername; private EditText editTextPassword;

Android Retrofit - POST request doesn't work, but in Postman it works

天大地大妈咪最大 提交于 2019-12-23 16:40:32
问题 This is the error I'm getting: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ JSON response I'm getting from Postman: { "title": "test title", "body": "test body", "userId": 1, "id": 101 } This is how I echoed the response from the rest API (used slim2 framework): $app->post('/posts', function() use($app){ $res = array( "title" => $app->request->post('title'), "body" => $app->request->post('body'), "userId"

NetworkOnMainThreadException with retrofit-beta2 and rxjava

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 15:48:36
问题 I recently upgraded from retroft-beta1 and this was working. I have the following interface for the API: public interface Service { @POST("path") Observable<Object> service(); } And the following call: service.service() .observeOn(AndroidSchedulers.mainThread()) .subscribe(); And it throws a NetworkOnMainThreadException. But this was working in retrofit-beta1. 回答1: From retrofit-beta2, calls to Observable methods now behave synchronously. So subscribeOn must be used: service.service()

Reading JSON with Retrofit

回眸只為那壹抹淺笑 提交于 2019-12-23 15:21:42
问题 I've got the following JSON feed: { collection_name: "My First Collection", username: "Alias", collection: { 1: { photo_id: 1, owner: "Some Owner", title: "Lightening McQueen", url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg" }, 2: { photo_id: 2, owner: "Awesome Painter", title: "Orange Plane", url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg" } } } What I am trying to do is get the contents of the collection - photo_id, owner, title and URL. I

Retrofit 2.0: getting response code 200 but not getting the desired data

旧城冷巷雨未停 提交于 2019-12-23 12:50:37
问题 A very disappointing feature of Retrofit 2.0 is that it does not exactly tell where it fails in parsing the response. Hence, In postman when I hit the request with same body, I get a login response as: { "result": "success", "response_code": 200, "data": { "id": "1", "display_name": "admin", "email": "payal@teckmovers.com", "username": "admin", "access_token": "8daa8e02ca432e51ae90912fbf63eeea" } } But when I hit the exact same request with exactly the same body in Retrofit, I get a very

Retrofit @Body showing up as parameter in HTTP request

有些话、适合烂在心里 提交于 2019-12-23 11:00:50
问题 I've previously used Square's Retrofit successfully for a @GET web API call but when trying to send JSON as the @BODY in a @POST call, on the server (Rails) the JSON is showing up as Parameters rather than the body request. My understanding is that @BODY will add that method parameter to the request in the body. Any idea what I'm doing wrong? WebApi : @POST("/api/v1/gear/scans.json") Response postScans( @Header(HEADER_AUTH) String token, @Body JsonObject scans ); Make web request: RestAdapter

RXJava how to try to get next after x time

纵饮孤独 提交于 2019-12-23 10:02:29
问题 I wan to do a call to a web service using retrofit every x seconds until y condition is raised. I want to run OrderApi.get after x seconds until the response is null. public class OrderApi {} public static Observable<Order> get() { //... } } OrderApi.get(order.getId())) .subscribe(updatedOrder -> { mShouldRun = updatedOrder != null; }); Already saw operators like Observable.delay Observable.timber but I cant find a way to use them properly. 回答1: this should work Observable.interval(1,

Retrofit image upload using Base64 in android

烈酒焚心 提交于 2019-12-23 09:34:50
问题 I am trying to upload the image using retrofit in Base64 format. To convert bitmap to Base64, public static String convertImageToStringForServer(Bitmap imageBitmap){ ByteArrayOutputStream stream = new ByteArrayOutputStream(); if(imageBitmap != null) { imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] byteArray = stream.toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); }else{ return null; } } I do not want to upload the image using Typedfile. My request