1

I'm trying to list a call from my server, but it comes empty. On postman I receive all the list, but I can't make it on android.

enter image description here

I have a Model Class :

public class Model {
@SerializedName("Title")
private String Title;
@SerializedName("Price")
private BigDecimal Price;

and the Call from my Interface :

@GET("api/Host/GetAllEvents")
Call<List<Model>> ListAllEvents();

My Service Class :

@Override
public void getAllEvents(onFinishedListener onFinishedListener) {
    Call<List<Model>> call = retrofitEndpoint.ListAllEvents();
    call.enqueue(new Callback<List<Model>>() {
        @Override
        public void onResponse(@NotNull Call<List<Model>> call, @NotNull Response<List<Model>> response) {
            onFinishedListener.onFinished(response.body());
        }

        @Override
        public void onFailure(@NotNull Call<List<Model>> call, @NotNull Throwable t) {
            onFinishedListener.onError(t);
        }
    });
}

I receive the list size but with the properties null.

And my Retrofit client class :

 private static final String BASE_URL = "http://192.168.0.101:3000";
private static Retrofit retrofit = null;

private static Gson gson = new GsonBuilder()
        .create();



public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(defaultClient())
                .build();
    }
    return retrofit;
}

private static OkHttpClient  defaultClient() {
    return new OkHttpClient().newBuilder()
            .connectTimeout(15, TimeUnit.SECONDS).
                    readTimeout(15, TimeUnit.SECONDS).
                    writeTimeout(15, TimeUnit.SECONDS).
                    addInterceptor(chain -> {
                        Request newRequest  = chain.request().newBuilder()
                                .addHeader("Authorization", "Bearer " + Hawk.get("t"))
                                .build();
                        return chain.proceed(newRequest);
                    }).build();
}
Nathiel Paulino
  • 534
  • 6
  • 17
  • 1
    try add the HttpLoggingInterceptor to easy to debug. https://stackoverflow.com/questions/32514410/logging-with-retrofit-2 – Linh Jul 15 '19 at 05:29

3 Answers3

5

Your model class:

public class Model {
@SerializedName("Title")
private String Title;
@SerializedName("Price")
private BigDecimal Price;

I see that you have serialized names capitalized here and it is all lower case in your json response.

Change them to lower case and try again:

public class Model {
@SerializedName("title")
private String Title;
@SerializedName("price")
private BigDecimal Price;
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
0

Try it out, think this way

change

@GET("api/Host/GetAllEvents")

to

@GET("/api/Host/GetAllEvents")

or

private static final String BASE_URL = "http://192.168.0.101:3000";

to

private static final String BASE_URL = "http://192.168.0.101:3000/";
0

Change the @SerializedName("Title") to @SerializedName("title")

and

Also change the @SerializedName("Price") to @SerializedName("price")

Because in the JSON title and price is lower case. But If you don't use @SerializedName annotation, you can just use the variable just as same as the name of your JSON.

public class Model {
  private String title;
  private BigDecimal price;
}

Change the base URL to

private static final String BASE_URL = "http://192.168.0.101:3000/";
Android
  • 1,420
  • 4
  • 13
  • 23