0

I want to retrieve a Json from server and pass it to WebView and consume it in a JavaScript function. So I think I dont need a Java class to hold the data and consume it in Java environment. Actually I want to omit the mediator calss (Users) I used in Retrofit. It was complicated for me to understand the structrure of retrofit request so I have no idea how to consume the response directly without a mediator class. This is my current code:

in Main Activity:

  public void getUserList(){
        GetUsers users = RetrofitInstance.getRetrofitInstance().create(GetUsers.class);
        Call<Users> call = users.getUserList("myUsername");
        call.enqueue(new Callback<Users>() {
            @Override
            public void onResponse(Call<Users> call, Response<Users> response) {
                PassUsersToJavaScript(response.body().getList());
            }

            @Override
            public void onFailure(Call<Users> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void PassUsersToJavaScript(String users){
        //Here I pass users to WebView
    }

This is the mediator Users class that I want to omit:

public class Users {

    @SerializedName("users")
    private String users;

    public Users(String users) {
        this.users = users;
    }
    public String getList(){
        return users;
    }
}

and this is the mediator Interface GetUsers :

public interface GetUsers {
    @GET("list/")
    Call<Users> getUserList(@Query("user") String user);
}

How can I omit the class Users.java and use the response.body() driectly in the line below:

  public void onResponse(Call<Users> call, Response<Users> response) {
            PassUsersToJavaScript(response.body().getList());
        }
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • Does this answer your question? [Is it possible to use retrofit without model class?](https://stackoverflow.com/questions/40503343/is-it-possible-to-use-retrofit-without-model-class) – Ali Sheikhpour Nov 24 '19 at 13:18

1 Answers1

0

I found that I can assign retrofit to a class, object, page and even a string. The model class users in the question above can be replaced with anything else like this JsonObject directly:

public void getUserList(){
        GetUsers users = RetrofitInstance.getRetrofitInstance().create(GetUsers.class);
        Call<JsonObject> call = users.getUserList("myUsername");
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                PassUsersToJavaScript(response.body().getList());
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });
    }
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82