1

I'm new working with retrofit in Android. I create a service using IIS where I load a JSON like this:

 [{"Id":1,"Name":"Product1","Price":9.99}, 
 {"Id":2,"Name":"Product2","Price":19.99}, 
 {"Id":3,"Name":"Product3","Price":29.99}, 
 {"Id":4,"Name":"Product4","Price":39.99}, 
 {"Id":5,"Name":"Product5","Price":49.99},
  ...]

I'm able to use the retrofit instruction @GET to bring some specific data. But when I try to implement @QUERY or @HEADER the results I receive are not what I expect. For example:

I have my interface:

public interface ServiceProduct {
@GET("api/Product/{id}")
Call<ManagerProduct> getFindShop(@Path(value="id") String productID);

@GET("api/Product")
Call<ManagerProduct> getHeader(@Header("Name") String header);
}

My ManagerProduct.java class is this:

public class ManagerProduct {

@SerializedName("Id")
@Expose
private Integer id;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("Price")
@Expose
private Double price;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

The way I'm calling the Service is using retrofit:

private void LoadProduct() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    ServiceProduct serviceProduct = retrofit.create(ServiceProduct.class);
    Call<ManagerProduct> listProduct = serviceProduct.getHeader("Product2");

    listProduct.enqueue(new Callback<ManagerProduct>() {
        @Override
        public void onResponse(Call<ManagerProduct> call, 
   Response<ManagerProduct> response) {
            if (response.isSuccessful()) {

                listManagerProduct=response.body();
                for (int i=0;i<listManagerProduct.size();i++){

           System.out.println(listManagerProduct.get(i).getPrice());

                }

            }
        }

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

        }
    });


}

As I result I expected 19.99 but Instead, I receive 9.99,19.99,29.99,39.99,49.99.

What I'm doing wrong?

As additional information, searching for information I found this question: retrofit 2 @path Vs @query

With the answer:

Consider this is the URL:

  www.app.net/api/searchtypes/862189/filters?Type=6&SearchText=School

Now this is the call:

 @GET("/api/searchtypes/{Id}/filters")
 Call<FilterResponse> getFilterList(@Path("Id") long customerId,
      @Query("Type") String responseType,
      @Query("SearchText") String searchText);

So we have:

  www.app.net/api/searchtypes/{Path}/filters?Type={Query}&SearchText={Query}

Things that come after the ? are usually queries.

If I try to write the URL and query directly in the browser:

 http://server/api/Product?Name=Product2

I received the full JSON. So I don't know if I implement wrong the service. I'm totally lost I have read a lot and I don't understand.

Any help, please?

Ramana V V K
  • 1,245
  • 15
  • 24
Roga Lu
  • 162
  • 1
  • 3
  • 14

0 Answers0