Not receiving json data with a REST call

Viewed 449

I am trying to consume my own REST call using retrofit. My only clue on why it doesn't work is that it says that the type of information is "text/html" even though I am sure it's json. That being said I haven't found any answers that solve my problem.

Payload:

  [
    {
        "user_id": "32",
        "username": "Marko",
        "last_activity": "2020-04-26 20:44:00",
        "user_image": "slika2"
    },
    {
        "user_id": "33",
        "username": "dejan",
        "last_activity": "2020-04-26 20:44:00",
        "user_image": "slika3"
    }
]

My chat class:

public class Chat {

    @SerializedName("user_id")
    private String userId;

    private String username;

    @SerializedName("last_activity")
    private String lastActivity;

    @SerializedName("user_image")
    private String userImage;\

    ...constructor/getters

   }

Api Interface:

@FormUrlEncoded
@POST("api_get_all_chats.php")
Call<List<Chat>> getChats(
        @Field("id") String id
);

Api client:

public static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.level(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        retrofit = new Retrofit.Builder()
                .baseUrl("http://MYIP/emob%20projekat/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        return retrofit;
    }

And the whole request:

        apiInterface = ApiClient.getClient().create(userApi.class);

        Call<List<Chat>> call = apiInterface.getChats(u.getUserInfo().getUserId());

        call.enqueue(new Callback<List<Chat>>() {
            @Override
            public void onResponse(Call<List<Chat>> call, Response<List<Chat>> response) {
                chatList = new ArrayList<>(response.body());
                chatAdapter = new ChatAdapter(getApplication().getApplicationContext(), R.layout.user_view, chatList);
                listView.setAdapter(chatAdapter);
            }

            @Override
            public void onFailure(Call<List<Chat>> call, Throwable t) {
                Toast.makeText(getApplication().getApplicationContext(), "ne valja" , Toast.LENGTH_LONG).show();

            }
        });

Any type of a clue where I am messing up would be much appreciated.

5 Answers

You base url has space when I decoded it I got

http://MYIP/emob projekat/api/

i recommend you to put base URL without encoding

How about cleartextTrafficPermitted="true" on your AndroidManifest. I see you code is fine and I think maybe the problem about your URL with a http scheme.

You can try to specify what response your client expects with Accept:application/json either in you API Interface with:

@FormUrlEncoded
@Headers({"Accept:application/json"})
@POST("api_get_all_chats.php")
Call<List<Chat>> getChats(
        @Field("id") String id
);

Either in your API client with new Retrofit.Builder().addHeader("Accept","application/json");.

This issue in square/retrofit is very similar to yours (only the inverse, since in the issue it is the server that does not accepts text/html while in your case it seems to be your client. Maybe, as in the issue, you are employing gson as well which cannot parse the text/html flavored response)

You need an interceptor like this.

public class CustomInterceptor extends Interceptor {

    @override 
    public Response intercept(Interceptor.Chain chain) {    
        return chain.proceed(chain.request().newBuilder().addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build())
    }
}
Related