I have the usual json server response. Example: {"id": 58339}

But when you start the application, an error occurs that the expected array, but in the response object. As far as I understand, the application needs an array of objects (example: [{"id": 58339}] ) How can I work with the usual json ( {"id": 58339} )? Example code: @GET("/api/catalog/get-product") Call<List<ProductModel>> getProduct(@Query("id") String id);

  private List<ProductModel> product; public String productId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product); Intent intent = getIntent(); this.productId = intent.getStringExtra(CatalogAdapter.PRODUCT_ID); App.getApi().getProduct(this.productId).enqueue(new Callback<ProductModel>() { @Override public void onResponse(Call<List<ProductModel>> call, Response<List<ProductModel>> response) { if (response.body() !=null) { product.addAll(response.body()); } } @Override public void onFailure(Call<List<ProductModel>>call, Throwable t) { Toast.makeText(ProductActivity.this, "Нет соединения с интернетом", Toast.LENGTH_SHORT).show(); } }); } 

And the model itself

  import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.util.List; public class ProductModel { @SerializedName("id") @Expose private int id; public int getId() { return id; } } 

    1 answer 1

    You request an array by specifying List<ProductModel> , since you need to request one object from just the ProductModel .

    • Good. I do this Call <ProductModel> in the request and in the controller. But I don’t understand how to load data into the model in the controller / I used to load data in this way private List <ProductModel> product; product.addAll (response.body ()); - Vitaut Hryharovich
    • did not understand the question - Komdosh
    • one
      @VitautHryharovich, without All - you received and add one product: product.add(response.body()); - woesss
    • Now this error is java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add (java.lang.Object)
    • you have this list not initialized, it is null, how do you write the code? - Komdosh