retrofit

Android Retrofit: Can't convert the returned JSON reponse to the specified model class when response code != 200

十年热恋 提交于 2020-01-15 10:56:53
问题 JSON when success code == 200 { "data": { "user": { "id": 8, "name": "soleekuser", "email": "soleekuser@gmail.com", "job_title": "back end developer", "active": 1, "profile_pic": "http://52.174.22.188/soleekappapi/public/storage/profile_pics/Capture_1544361563.jpeg", "created_at": "2018-12-09 13:20:37" }, "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

Best way to decouple REST call from `Activity`

对着背影说爱祢 提交于 2020-01-15 07:34:05
问题 I am using Retrofit and want REST calls to survive when the user navigates between my activities (as opposed to just retaining them accross configuration changes). Couple of options: Retrofit syncronous call within an AsyncTask and put that in a retained "task fragment" Retrofit syncronous call within an IntentService and broadcast Google suggests a per-app singleton, at least with Volley... What is the best approach? And I can see RxJava as a Retrofit mode but even there one is just adviced

My app freezes when I make a network request in background thread (using CoroutineWorker & Work manager)

邮差的信 提交于 2020-01-14 05:32:48
问题 I'm trying to set wallpaper on a periodic basis and for that, I'm using the Work manager. When I make a network request from a background thread (CoroutineWorker), my app UI freezes till network request & set wallpaper completes. I've already tried to run this using Async Task as well, but no use. @WorkerThread override suspend fun doWork(): Result = coroutineScope { try { MainService.createRetrofitService().getRandomPhoto(map) .enqueue(object : Callback<RandomPhotoModel> { override fun

Is it possible to manipulate retrofit url without method parameter?

感情迁移 提交于 2020-01-14 04:30:09
问题 Network call are made like this @GET("/api/video/{slug}/show") void getVideoData(@Path("slug") String slug,Callback<VideoContainerGSON>cb); Now I need to add wariable path before "/api" e.g: /en/api/video/{slug}/show Or /sp/video/{slug}/show That parameter is global wide, so without setting alteration all network call will use same language path. Question: Is there a way to alter it without method signature or I must change method signature to @GET("/{lang}/api/video/{slug}/show") void

Building a library that uses Retrofit internally, wrapping responses

旧时模样 提交于 2020-01-14 03:09:08
问题 I'm trying to build a library that basically wraps our api. Basically, the structure im going for is something like this: MySDK mySDK = new MySDK("username", "password"); mySDK.getPlaylistInfo("3423", 2323, new CustomCallback<>(){ //on response //on failure }); So with vanilla Retrofit, an api call usually looks something like the following: ApiService api = retrofit.create(ApiService.class); Call<Response> call = api.getPlaylistInfo() call.enqueue(new Callback<Response>() { @Override public

Retrofit generic response handler

随声附和 提交于 2020-01-13 13:52:50
问题 I wish to handle all my responses in single method. the purpose is to recall the service when the response code is not 3, when the response code is 3 I intend to first refresh the token and then recall the same service. I've created a Basecallback class to catch one method but I can't see the log and can't catch breakpoints. BASECALLBACK.class public class BaseCallBack<T> implements Callback<T> { @Override public void onResponse(Call<T> call, Response<T> response) { if (!response.isSuccessful

Retrofit generic response handler

南笙酒味 提交于 2020-01-13 13:52:26
问题 I wish to handle all my responses in single method. the purpose is to recall the service when the response code is not 3, when the response code is 3 I intend to first refresh the token and then recall the same service. I've created a Basecallback class to catch one method but I can't see the log and can't catch breakpoints. BASECALLBACK.class public class BaseCallBack<T> implements Callback<T> { @Override public void onResponse(Call<T> call, Response<T> response) { if (!response.isSuccessful

Add Retrofit Requestinterceptor with Dagger at runtime

泄露秘密 提交于 2020-01-13 13:52:13
问题 I'm using dagger and retrofit. I inject my Retrofit services with Dagger. Now i wanna do a authorization request to get an accessToken. Afterwards i want to enhance my api module with an Request interceptor to use this access token for future requests. My idea is to use the ObjectGraph.plus() method after i received the access token, but i'm not sure if this is the best way to do it. Can someone point me to the right direction or maybe is there an example project on github ? 回答1: The key is

RxJava alternative for map() operator to save emitted items

末鹿安然 提交于 2020-01-13 10:07:09
问题 I use Retrofit to interact with a REST API and RxJava do manipulate the data I receive. In the code snippet below I make an API call and use the map operator to save the data I receive before moving on with other operations against the stream. retrofitApi.registerDevice(mDevice) .map(new Func1<Device, Device>() { @Override public Device call(Device device) { // The only purpose of the map() operator // is to save the received device. magicBox.saveDeviceId(device.getId()); return device; } })

Retrofit2

馋奶兔 提交于 2020-01-13 07:10:59
【前言】 平时开发过程中少不了远程接口调用,常用的工具类包括:HttpClient、HttpURLConnection、OKHttp和Volley,区别可参考下面链接文章 HTTP客户端连接,选择HttpClient还是OkHttp? 网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比 本文主要介绍基于OKHttp的网络请求框架: Retrofit 【简介】 Retrofit 是一个Square开发的类型安全的REST安卓客户端请求库。这个库为网络认证、API请求以及用OkHttp发送网络请求提供了强大的框架 。Retrofit 可以利用接口,方法和注解参数来声明式定义一个请求应该如何被创建。并且可更换或自定义HTTP client,以及可更换或自定义Converter,返回数据解析方式。Retrofit可用于Android和Java的一个类型安全(type-safe)的REST客户端,如果你的服务器使用的使RESTAPI,那么你将非常适合使用它。 【GitHub】 https://github.com/square/retrofit 【Maven】 https://mvnrepository.com/artifact/com.squareup.retrofit2 本文以2.6.2为例 < dependency > <