Retrofit different call and response json bodies - java

How can be done retrofit PUT with different types of call and responce bodies?
I have a limitation with error. Types must be identical <T> and <T>.
Responce bodie can contain int value, but call should not, because when i initialize CallBody object with int value, it already contain n1=0, and send this parameter to server.
#PUT("/api/test")
Call<CallBody> test1(#Body CallBody params_body);
public class CallBody {
public String id;
}
public class ResponceBody {
public String s1;
public int n1;
}
public void Test(String id) {
CallBody params_body = new CallBody();
params_body.id = id;
Call<CallBody> call1 = apiInterface.test1(params_body);
call1.enqueue(new Callback<CallBody>() {
#Override
public void onResponse(Call<CallBody> call, Response<ResponceBody> response) {
ResponceBody response = response.body();
}
#Override
public void onFailure(Call<CallBody> call, Throwable t) {
call.cancel();
}
});
}

Try,
public class ResponceBody {
public String s1;
public Integer n1;
}

Related

How to create a polymorphic structure that can work for both success and error responses in Java?

I'm working on a class that will get a list of strings and process them asynchronously using CompletableFutures. Each string is processed by invoking another class that will perform several operations and return a response or throw an exception if there is an error.
I would like to aggregate the responses that I get, whether they have a valid response or an exception and return them as a list to the caller. I would like the caller to be able to expect a list of SomeResponse and be able to interpret them using polymorphism.
However, I'm stuck on determining if this can be done using polymorphism at all, given that the fields for the success and error response are completely different. I have added some pseudo code below on one alternative I have thought of. Basically have SomeResponse be an interface with an isSuccess method. This will allow the caller to know if it's an error or not. However, the caller would still have to cast it to the correct implementation in order to get the value or the error. Is there a better way to approach this? My requirement is being able to return both a success and error response for each given request in the list. If there is an exception, we don't want to abort the entire operation.
public MyProcessorClass {
private final SomeOtherClass someOtherClass;
public List<SomeResponse> process(List<String> requestList) {
return requestList.stream().map(this::procesRequest)
.collectors(Collect.tolist()):
}
private processRequest(String request) {
CompletableFuture completableFuture = CompletableFuture
.supplyAsync(() => {
return new SomeSuccessResponse(someOtherClass.execute(request));
})
.exceptionally(e -> {
return new SomeErrorResponse(e.getCause);
});
return completableFuture.get();
}
}
public interface SomeResponse {
boolean isSuccess();
}
public class SomeSuccessResponse implements SomeResponse {
private final String value;
#Getter
private final boolean success;
public SomeSuccessResponse(String value) {
this.value = value;
this.success = true;
}
}
public class SomeErrorResponse implements SomeResponse {
private final Throwable error;
#Getter
private final boolean success;
public SomeErrorResponse(Throwable error) {
this.error = error;
this.success = false;
}
}
What you want is the visitor pattern https://en.wikipedia.org/wiki/Visitor_pattern
public class Main {
interface IResponse {
void acceptHandler(IResponseHandler handler);
}
static class ResponseA implements IResponse {
#Override
public void acceptHandler(IResponseHandler handler) {
handler.handle(this);
}
}
static class ResponseB implements IResponse {
#Override
public void acceptHandler(IResponseHandler handler) {
handler.handle(this);
}
}
public interface IResponseHandler {
void handle(ResponseA response);
void handle(ResponseB responseB);
}
public static void main(String[] args) {
final IResponseHandler handler = new IResponseHandler() {
#Override
public void handle(ResponseA response) {
System.out.println("Handle ResponseA");
}
#Override
public void handle(ResponseB responseB) {
System.out.println("Handle ResponseB");
}
};
final IResponse someResponse = new ResponseA();
someResponse.acceptHandler(handler);
}
}

Expected BEGIN_ARRAY but was BEGIN_OBJECT in Retrofit

I am new to android programming and can anyone help me or point out why its giving me this error
I want to fetch some data from the server such as under the Hardware json and get the names and status, but when i call api its shows me this.
Change the line
public void onResponse(Call<List<ObjectList>> call, Response<List<ObjectList>> response) {
to
public void onResponse(Call<List<ObjectList>> call, Response<ObjectList> response) {
As per your code, you are expecting response as List. But Actual response is object. So, you need to generate model class based on your response and set in code for output.
Your Model should be like :
public class Application {
ArrayList<Object> hardware = new ArrayList<Object>();
Header HeaderObject;
ArrayList<Object> software = new ArrayList<Object>();
// Getter Methods
public Header getHeader() {
return HeaderObject;
}
// Setter Methods
public void setHeader( Header headerObject ) {
this.HeaderObject = headerObject;
}
}
public class Header {
Stamp StampObject;
private String frame_id;
private float seq;
// Getter Methods
public Stamp getStamp() {
return StampObject;
}
public String getFrame_id() {
return frame_id;
}
public float getSeq() {
return seq;
}
// Setter Methods
public void setStamp( Stamp stampObject ) {
this.StampObject = stampObject;
}
public void setFrame_id( String frame_id ) {
this.frame_id = frame_id;
}
public void setSeq( float seq ) {
this.seq = seq;
}
}
public class Stamp {
private float secs;
private float nsecs;
// Getter Methods
public float getSecs() {
return secs;
}
public float getNsecs() {
return nsecs;
}
// Setter Methods
public void setSecs( float secs ) {
this.secs = secs;
}
public void setNsecs( float nsecs ) {
this.nsecs = nsecs;
}
}
Then change below line :
public void onResponse(Call<List<ObjectList>> call, Response<Application> response) {
Change this:
#GET("system_monitor")
Call<List<ObjectList>> getHardware();
to
#GET("system_monitor")
Call<ObjectList> getHardware();
Your response return an object instead of array.
Instead of
#GET("system_monitor")
Call<List<ObjectList>> getHardware();
use
#GET("system_monitor")
Call<ObjectList> getHardware();
And then use it like below:
Call<ObjectList> call = webRequestAPI.getHardware();
call.enqueue(new Callback<ObjectList>() {
#Override
public void onResponse(Call<ObjectList> call, Response<ObjectList> response) {
if (!response.isSuccessful()) {
textViewHardwareName.setText("Code: " + response.code());
return;
}
ObjectList system_monitor = response.body();
...
}
#Override
public void onFailure(Call<ObjectList> call, Throwable t) {
textViewHardwareName.setText(t.getMessage());
}
});
The best thing for your scenario hardware and Software are as objects , which have two property
1.Name 2. Object status.
So I recommend you to create a class name as System and put there these two variables so finally your class looks like :
Class System
{
String object_name;
boolean object_status;
}
and your getter setter .
And update your model class like this
#SerializedName("hardware")
#Expose
public List<System> hardware;
#SerializedName("software")
#Expose
public List<System> software;
and change your retrofit response holder as.
public void onResponse(Call<List<ObjectList>> call, Response<ObjectList>
response) {

Retrofit 2 - RxJava : Unable to invoke no-args constructor for retrofit2.Call<RemoteDataObjectModel>

I am creating an Android app and the objective is to display a list of Pokemon which can be found using the PokeApi at https://pokeapi.co/
I have two instances, one where it works without RxJava2 and one where it doesn't work with RxJava 2. For both instances I use Retrofit 2.
For when it does not work when I include RxJava2 the error that I recieve is
D/thrown: java.lang.RuntimeException: Unable to invoke no-args constructor for retrofit2.Call<za.co.lbnkosi.discoveryassesment.domain.model.RemoteDataObjectModel>. Registering an InstanceCreator with Gson for this type may fix this problem.
At this point I have looked through a lot of Stackoverflow questions similar to this one and most if not all of them mention deserialization which for me has not worked this far.
I would like to know what the problem is or what I am doing wrong and how I can fix this issue. Below I have included the relevant code
public interface PokeApi {
//Ignore
#GET("pokemon")
Call<RemoteDataObjectModel> getPokemonList(#Query("limit") int limit, #Query("offset") int offset);
#GET("pokemon")
Observable<Call<RemoteDataObjectModel>> getPokemonList2(#Query("limit") int limit, #Query("offset") int offset);
}
public class RemoteDataObjectModel {
#SerializedName("results")
private ArrayList<RemoteDataModel> results;
public ArrayList<RemoteDataModel> getResults() {
return results;
}
public void setResults(ArrayList<RemoteDataModel> results) {
this.results = results;
}
}
public class RemoteDataModel {
#SerializedName("number")
private int number;
#SerializedName("name")
private String name;
#SerializedName("url")
private String url;
public int getNumber() {
String[] urlItems = url.split("/");
return Integer.parseInt(urlItems[urlItems.length -1]);
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
public class RetrofitComponent {
private static RetrofitComponent INSTANCE;
private PokeApi pokeApi;
private RetrofitComponent(){
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://pokeapi.co/api/v2/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
pokeApi = retrofit.create(PokeApi.class);
}
public static RetrofitComponent getInstance() {
if (INSTANCE == null) {
INSTANCE = new RetrofitComponent();
}
return INSTANCE;
}
public Observable<Call<RemoteDataObjectModel>> getPokemonList(int limit, int offest) {
return pokeApi.getPokemonList2(30,0);
}
}
private void getPokemonList(PokeApiDataSource.PokemonListCallback callback) {
RetrofitComponent.getInstance()
.getPokemonList(100,0)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Call<RemoteDataObjectModel>>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(Call<RemoteDataObjectModel> remoteDataObjectModelCall) {
Log.d("","");
remoteDataObjectModelCall.enqueue(new Callback<RemoteDataObjectModel>() {
#Override
public void onResponse(#NotNull Call<RemoteDataObjectModel> call, #NotNull Response<RemoteDataObjectModel> response) {
loading = true;
RemoteDataObjectModel pokeApiObjects = response.body();
_arrayList = Objects.requireNonNull(pokeApiObjects).getResults();
callback.pokemonListSuccess();
}
#Override
public void onFailure(#NotNull Call<RemoteDataObjectModel> call, #NotNull Throwable t) {
loading = true;
Log.e(TAG, " onFailure: " + t.getMessage());
}
});
}
#Override
public void onError(Throwable e) {
Log.d("thrown", e.toString());
}
#Override
public void onComplete() {
}
});
}
I think Call is the class from Retrofit. It provides a callback function to get the response asynchronously. But since you are going to use RxJava, the nature of Rxjava is already asynchronous. You may not need to get the response as Call. Instead, please try this
public interface PokeApi {
// If you need to get the response body + headers ...
#GET("pokemon")
Observable<Response<RemoteDataObjectModel>> getPokemonList2(#Query("limit") int limit, #Query("offset") int offset);
// If you only need body
#GET("pokemon")
Observable<RemoteDataObjectModel> getPokemonList2(#Query("limit") int limit, #Query("offset") int offset);
// Or the better way, the result from API is only return once. So, Single is more suitable in this case
#GET("pokemon")
Single<RemoteDataObjectModel> getPokemonList2(#Query("limit") int limit, #Query("offset") int offset);
}

Retrofit2, Android, #Get Parsing for the Array of Array

I am parsing the title correctly and displaying it in a listview. I cant seem to access the
String findMe;
seen below. Here are the two objects, sample response and my call.
response pojo:
public class Response {
public String count;
public Result []results;
}
first object
public class Result {
public String title;
public static arr [] Details;
second object
public class Details {
public Integer _id;
public String findMe;
}
response:
Call<Response> call = api.getListWith(API_KEY);
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, Response<Response> response) {
result = response.body();
}
I am getting the title just by passing 'result' into the adapter and using
result[i].getTitle();
I tried using
result[i].Details[0].findMe;
but my error response is:
java.lang.NullPointerException: Attempt to read from null array
public class Result {
public String title;
public Details[] arr;
}
Then
results[i].getarr[0].getfindMe();

Generics overloading of method - workaround

this question was asked many times but I couldn't find elegant workaround for it.
This example works as desired:
public class RequestWrapper<T> {
private final T request;
private final Class<T> type;
public RequestWrapper(T request, Class<T> type) {
this.request = request;
this.type = type;
}
public T getRequest() {
return request;
}
public Class<T> getType() {
return type;
}
}
public class Service {
private void invoke(String request) {
System.out.println("String:" + request);
}
private void invoke(Object request) {
System.out.println("Object:" + request + "," + request.getClass().getSimpleName());
}
public static void main(String[] args) {
RequestWrapper<String> sw = new RequestWrapper<String>("A", String.class);
RequestWrapper<Integer> iw = new RequestWrapper<Integer>(Integer.valueOf(0), Integer.class);
new Service().invoke(sw.getRequest());
new Service().invoke(iw.getRequest());
}
}
But I would need to add one more method to Service class which do something before/after call of invoke method:
public void invoke(RequestWrapper<?> wrapper) {
try {
// ...
invoke(wrapper.getType().cast(wrapper.getRequest()));
invoke(wrapper.getRequest());
} catch(Exception e ) {
// ...
}
}
then the main method would contain:
new Service().invoke(sw);
I understand the reason why the invoke(Object request) is used instead of invoke(String request).
What would be an elegant solution to call proper invoke method and be able to do some common actions before/after it?
To have an interface e.g. Invoker, implement it e.g. StringInvoker, Invoker> and call map.get(wrapper.getType()).invoke(wrapper.getRequest()) is possible solution but I expect something better.
You can check the type and explicitly cast it, for example (I also added Integer so you can see branching on more types):
Class<?> c = wrapper.getType();
if (c == String.class)
invoke((String) wrapper.getRequest()); // Invokes invoke(String)
else if (c == Integer.class)
invoke((Integer) wrapper.getRequest()); // Invokes invoke(Integer)
else
invoke(wrapper.getRequest()); // Invokes invoke(Object)
Note:
If you go on this path, you don't even need to store the request type in the RequestWrapper class because you can just as easily use the instanceof operator on the request itself to check its type. And if you "get rid" of the request type, your current RequestWrapper class will only contain the request so the RequestWrapper is not even needed in this case.
Visitor patter can serves to solve it. Only drawback is that there isn't possible to write:
new Service().invoke(new RequestWrapper<String>("A"));
My implementation:
public class Service {
public void invoke(RequestWrapper<?> wrapper) {
try {
// ...
wrapper.invoke(this);
} catch(Exception e ) {
// ...
}
}
public void invoke(String request) {
System.out.println("String:" + request);
}
public void invoke(Boolean request) {
System.out.println("Boolean:" + request);
}
public static void main(String[] args) {
RequestWrapper<Boolean> rw = new BooleanRequestWrapper(Boolean.TRUE);
new Service().invoke(rw);
}
}
abstract class RequestWrapper<T> {
protected final T request;
public RequestWrapper(T request) {
this.request = request;
}
public abstract void invoke(Service v);
}
class BooleanRequestWrapper extends RequestWrapper<Boolean> {
public BooleanRequestWrapper(Boolean request) {
super(request);
}
public void invoke(Service service) {
service.invoke(request);
}
}

Categories