retrofit2

How to handle error in Retrofit 2.0

我们两清 提交于 2019-12-04 02:34:00
I want to handle error in Retrofit 2.0 Got e.g. code=404 and body=null , but errorBody() contains data in ErrorModel ( Boolean status and String info ). This is errorBody().content : [text=\n{"status":false,"info":"Provided email doesn't exist."}] . How can I get this data? Thank for helping me! This is my code for Retrofit request: ResetPasswordApi.Factory.getInstance().resetPassword(loginEditText.getText().toString()) .enqueue(new Callback<StatusInfoModel>() { @Override public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) { if (response.isSuccessful()) {

RXJava2: correct pattern to chain retrofit requests

别说谁变了你拦得住时间么 提交于 2019-12-04 02:23:00
I am relatively new to RXJava in general (really only started using it with RXJava2), and most documentation I can find tends to be RXJava1; I can usually translate between both now, but the entire Reactive stuff is so big, that it's an overwhelming API with good documentation (when you can find it). I'm trying to streamline my code, an I want to do it with baby steps. The first problem I want to solve is this common pattern I do a lot in my current project: You have a Request that, if successful, you will use to make a second request. If either fails, you need to be able to identify which one

Execute http request in parallel with Retrofit 2

ⅰ亾dé卋堺 提交于 2019-12-04 00:23:11
问题 I want to implement multiple parallel request in Retrofit 2. I have the following structure to make 3 request : HistoricalRApi.IStockChart service=HistoricalRApi.getMyApiService(); //^BVSP,^DJI,^IXIC Call<HistoricalDataResponseTimestamp> call1= service.get1DHistoricalDataByStock("^IXIC"); Call<HistoricalDataResponseTimestamp> call2= service.get1DHistoricalDataByStock("^DJI"); Call<HistoricalDataResponseTimestamp> call3= service.get1DHistoricalDataByStock("^GSPC"); call1.enqueue

Retrofit - android.os.NetworkOnMainThreadException

青春壹個敷衍的年華 提交于 2019-12-04 00:16:47
I am using Retrofit 2 to get json and parse it to POJO. My purpose is getting one value of that object. compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' My REST client: public interface MyClient { @GET("/part1/part2") Call<MyItem> getMyItem(@Query("param1") String param1, @Query("param2") String param2, @Query("param3") String param3); } Here I found great great tool to create service: public class ServiceGenerator { public static final String API_BASE_URL = "http://my.api.com"; private static OkHttpClient.Builder httpClient =

Add an array as request parameter with Retrofit 2

和自甴很熟 提交于 2019-12-04 00:06:24
问题 I'm looking for way to add an int array (e.g [0,1,3,5]) as parameter in a GET request with retrofit 2. Then, the generated url should be like this : http://server/service?array=[0,1,3,5] How to do this ? 回答1: Just add it as a query param @GET("http://server/service") Observable<Void> getSomething(@Query("array") List<Integer> array); You can also use int[], or Integer... as a last param; 回答2: I have finally founded a solution by using Arrays.toString(int []) method and by removing spaces in

Service Generator Retrofit

ε祈祈猫儿з 提交于 2019-12-03 21:41:01
Can someone help me understand the createService method in below code. I need to understand what is method's parameter Class S and the code underneath in depth public class ServiceGenerator { public static final String API_BASE_URL = Constant.BASE_URL; private static OkHttpClient httpClient = new OkHttpClient(); private static Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()); public static <S> S createService(Class<S> serviceClass) { httpClient.interceptors().add(new Interceptor() { @Override public Response intercept

NoSuchMethodError Lcom/google/gson/Gson; upon retrofit response

£可爱£侵袭症+ 提交于 2019-12-03 20:28:44
So, I've had my app published for nearly a year without seeing this issue, and now it shows up. Even right now, I don't have this issue with the debug version on my phone. I don't have any issues with any emulators opened from Android Studio. However nearly every emulator from the pre-launch reports in the Google Developer Console crashes with this NoSuchMethodError upon receiving Retrofit response. FATAL EXCEPTION: ControllerMessenger Process: xxxxxxx, PID: 25090 java.lang.NoSuchMethodError: No direct method <init>(Lcom/google/gson/Gson;)V in class Lcom/google/gson/Gson$1; or its super

How to handle response which can be different Type in Retrofit 2

允我心安 提交于 2019-12-03 17:05:35
In WebApi returned JSON's field can be of different class: { someField:"some string" } { someField: { "en" : "some string", "ka" : "რამე სტრინგი" } } I've seen some solutions, but it was on previous versions of Retrofit. How would my pojo class look like and what can i use to parse this dynamic json? For your case you can use Call<JsonElement> as response type and parse it in response: call.enqueue(new Callback<JsonElement>() { @Override public void onResponse(Call<JsonElement> call, Response<JsonElement> response) { if(response.isSuccessful()){ JsonElement jsonElement = response.body(); if

Retrofit add header with token and id

我怕爱的太早我们不能终老 提交于 2019-12-03 17:03:37
I have a problem with getting authenticated user. Before it I got token and user id. Now i need to get user from server using access token and id. I have header format Now I'am trying to add header with user token and id using interceptor. My code: Interceptor interceptor = new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("Accept", "application/json") .addHeader("authorization", token) <-?? .addHeader("driver_id", id) <-?? .build(); return chain.proceed(newRequest); } }; OkHttpClient

Retrofit OKHTTP Offline caching not working

拈花ヽ惹草 提交于 2019-12-03 16:36:39
I read dozens of tutorial and Stackoverflow answers to my problem but nothing is working for me! Also, most of them are old so probably OKHTTP changed somehow. All I want is to enable offline caching for Retrofit. I am using GET I tried using only offlineCacheInterceptor as an Interceptor, but I kept getting: Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname I tried using a combination of offlineCacheInterceptor as an Interceptor + provideCacheInterceptor() as a NetworkInterceptor, but I kept getting: 504 Unsatisfiable Request (only-if-cached) and a