0

Retrofit returns null whenever using with initialised integer but returns data when calling directly with integer number... I'm using @path to access the data of WordPress-rest-api

This is My interface

public interface ImageDataApi {

String BASE_URL = "https://dwarkawala.com/index.php/wp-json//wp//v2//";

@GET("media/{Id}")
Call<Thumbnail> getImageData(
        @Path("Id") int postId
);



class ImageFactory {
    private static ImageDataApi service;
    public static ImageDataApi getInstance() {

        if (service == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .build();
            service = retrofit.create(ImageDataApi.class);
            return service;
        } else {
            return service;
        }
    }
}

And i'm calling like this , it returns null all the time

int number = post.getId();
    ImageDataApi.ImageFactory.getInstance().getImageData(number).enqueue(new Callback<Thumbnail>() {
        @Override
        public void onResponse(Call<Thumbnail> call, retrofit2.Response<Thumbnail> response) {

            Log.d(TAG,String.valueOf(response.body()));


        }

        @Override
        public void onFailure(Call<Thumbnail> call, Throwable t) {

        }
    });

But when i call directly like this, it works...

ImageDataApi.ImageFactory.getInstance().getImageData(3658).enqueue(new Callback<Thumbnail>() {
        @Override
        public void onResponse(Call<Thumbnail> call, retrofit2.Response<Thumbnail> response) {

            Log.d(TAG,String.valueOf(response.body()));


        }

        @Override
        public void onFailure(Call<Thumbnail> call, Throwable t) {

        }
    });

I don't get it where the error happens please help...

3
  • Did you check the result of post.getId()? Might not be what you expect
    – Fred
    Mar 9, 2019 at 14:03
  • @Fred its gives same integer that i called directly
    – Rahul Shah
    Mar 9, 2019 at 14:06
  • Then I don't know what's happening. Everything seems to check out.
    – Fred
    Mar 9, 2019 at 14:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.