skip to Main Content

I am paring CollectionListing Api from Shopify and I need to pass 'collecton_listing_id' as parameter in url. I am passing the id but unable to get response in Callback.

Note: I have tested the api with parameters in Postman and works fine.

Here is my implementation.

ProductActivity.java

 private void processProductIds() {

    apiInterface = APIClient.getClient().create(ApiInterface.class);

    Call<List<Integer>> call = apiInterface.getProductIds(key, id);

    call.enqueue(new Callback<List<Integer>>() {
        @Override
        public void onResponse(Call<List<Integer>> call, Response<List<Integer>> response) {
            String s = String.valueOf(response.body());
            Log.d("S", "onResponse:" + s);
        }

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

        }
    });
}

ApiInterface.java

public interface ApiInterface {

@GET("collection_listings/{collection_listing_id}/product_ids.json")
Call<List<Integer>> getProductIds(@Header("Authorization") String accessToken, @Path("collection_listing_id") long id);

}

Model Class

public class IdExample {

@SerializedName("product_ids")
@Expose
private List<Integer> productIds = null;

public List<Integer> getProductIds() {
    return productIds;
}

public void setProductIds(List<Integer> productIds) {
    this.productIds = productIds;
 }
}

JSON Response

{
"product_ids": [
    1331092619350,
    1331125551190,
    1331126599766,
    1331121651798
  ]
}

Thank you.

3

Answers


  1. You need to change the return type of getProductIds from Call<List<Integer>> to Call<IdExample>

    EDIT: (as discussed in the comments)

    You should also change List<Integer> productIds to List<Long> productIds

    Login or Signup to reply.
  2. Try to call API using,

    mAPIService.functionpost("url/"+id).enqueue(new Callback<List<IdExample>>() {
            @Override
            public void onResponse(Call<List<IdExample>> call, Response<List<IdExample>> response) {
                if (response.isSuccessful()) {
                    if (constantsVariables.debug) {
                        Log.i(constantsVariables.TAG, "/****************************/" + response.raw());
    
                    }                 
            }
    
            @Override
            public void onFailure(@NonNull Call<List<IdExample>> call, @NonNull Throwable tr) {
                Log.d("TEST  Inside fail if", Log.getStackTraceString(tr));
            }
        });
    
    Login or Signup to reply.
  3. It should be like this

    private void processProductIds() {
    
    apiInterface = APIClient.getClient().create(ApiInterface.class);
    
    Call<IdExample> call = apiInterface.getProductIds(key, id);
    
    call.enqueue(new Callback<IdExample>() {
        @Override
        public void onResponse(Call<IdExample> call, Response<IdExample> response) {
            String s = String.valueOf(response.body().getProductIds());
            Log.d("S", "onResponse:" + s);
        }
    
        @Override
        public void onFailure(Call<IdExample> call, Throwable t) {
    
        }
    });
    }
    

    Then change api interface like this

    public interface ApiInterface {
    @GET("collection_listings/{collection_listing_id}/product_ids.json")
    Call<IdExample> getProductIds(@Header("Authorization") String 
    accessToken, @Path("collection_listing_id") long id);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search