Sending JSON in POST request with Retrofit2

Use these in gradle compile ‘com.squareup.retrofit2:retrofit:2.3.0’ compile ‘com.squareup.retrofit2:converter-gson:2.3.0’ compile ‘com.squareup.retrofit2:converter-scalars:2.3.0’ Use these two POJO class …….. LoginData.class public class LoginData { private String email; private String password; public LoginData(String email, String password) { this.email = email; this.password = password; } /** * * @return * The email */ public String getEmail() { return email; } … Read more

How can we handle different response type with Retrofit 2?

I had a similar issue and I solved it by using a generic Object and then testing what type of response I had using instanceof Call<Object> call = api.login(username, password); call.enqueue(new Callback<Object>() { @Override public void onResponse(Response<Object> response, Retrofit retrofit) { if (response.body() instanceof MyPOJO ) { MyPOJO myObj = (MyPOJO) response.body(); //handle MyPOJO } … Read more

how to run java and xml code (fetched from api) into another fragment

There two way to add view dynamically . you create you xml file .then using LayoutInflater you lnflater that xml to your view . Or You can create view dynamically like var textview=TextView() then textview.text=”xyz” You need relativelayout or linearlayout where will add those will using addView(); method. But i condition you have declare your … Read more

Retrofit 2 – URL Query Parameter

If you specify @GET(“foobar?a=5”), then any @Query(“b”) must be appended using &, producing something like foobar?a=5&b=7. If you specify @GET(“foobar”), then the first @Query must be appended using ?, producing something like foobar?b=7. That’s how Retrofit works. When you specify @GET(“foobar?”), Retrofit thinks you already gave some query parameter, and appends more query parameters using … Read more

How can I handle empty response body with Retrofit 2?

Edit: As Jake Wharton points out, @GET(“/path/to/get”) Call<Void> getMyData(/* your args here */); is the best way to go versus my original response — You can just return a ResponseBody, which will bypass parsing the response. @GET(“/path/to/get”) Call<ResponseBody> getMyData(/* your args here */); Then in your call, Call<ResponseBody> dataCall = myApi.getMyData(); dataCall.enqueue(new Callback<ResponseBody>() { @Override … Read more

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

Reason: This error occurs because jackson library doesn’t know how to create your model which doesn’t have an empty constructor and the model contains constructor with parameters which didn’t annotated its parameters with @JsonProperty(“field_name”). By default java compiler creates empty constructor if you didn’t add constructor to your class. Solution: Add an empty constructor to … Read more

Retrofit and RxJava: How to combine two requests and get access to both results?

As I understand – you need to make a request based on result of another request and combine both results. For that purpose you can use this flatMap operator variant: Observable.flatMap(Func1 collectionSelector, Func2 resultSelector) Returns an Observable that emits the results of a specified function to the pair of values emitted by the source Observable … Read more

Set dynamic base url using Retrofit 2.0 and Dagger 2

Support for this use-case was removed in Retrofit2. The recommendation is to use an OkHttp interceptor instead. HostSelectionInterceptor made by swankjesse import java.io.IOException; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; /** An interceptor that allows runtime changes to the URL hostname. */ public final class HostSelectionInterceptor implements Interceptor { private volatile String host; public … Read more