Search code examples
androidjsonandroid-studioviewmodelandroid-viewmodel

Request Queue in volley cannot be applied to View Model


Am trying to load my JSON data into view model class that extends android View Model and i cant get the context for the Request Queue

The View Model class that extends with the android view model will be used by an observer to load the product list into the product adapter onChanged method

Here is my view model class code


public class ProductViewModel extends AndroidViewModel {

    private MutableLiveData<List<Product>> productLiveData;
    private List<Product> productList;
    private Application application;

    private JsonArrayRequest productJsonArrayRequest;

    public ProductViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<List<Product>> getProductLiveData () {
        if(productLiveData == null) {
            productLiveData = new MutableLiveData<>();
            initialize();
        }
        return productLiveData;
    }

    private void initialize(){
        productJsonRequest();
        productLiveData.setValue(productList);
    }


    private void productJsonRequest() {

        productList = new ArrayList<>();


            JsonArrayRequest productsJsonArrayRequest = new JsonArrayRequest(Config.FETCH_PRODUCTS, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    JSONObject jsonObject;

                    for (int i = 0; i < response.length(); i++) {

                        try {
                            jsonObject = response.getJSONObject(i);
                            Product product = new Product();

                            product.setId(jsonObject.getInt("id"));
                            product.setName(jsonObject.getString("name"));
                            product.setPrice(jsonObject.getDouble("price"));
                            product.setStock(jsonObject.getInt("stock"));
                            product.setCategoryId(jsonObject.getInt("categoryId"));
                            product.setCategory(jsonObject.getString("category"));
                            product.setPhoto(jsonObject.getString("photo"));

                            productList.add(product);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });


            RequestQueue productRequestQueue = Volley.newRequestQueue(this);
            productRequestQueue.add(productsJsonArrayRequest);
    }
} 

The Problem is here

RequestQueue productRequestQueue = Volley.newRequestQueue(this);


Solution

  • Volley.newRequestQueue() requires a Context to be passed in. You're using the keyword "this" which refers to the view model class which is not a context. This is easy to fix, just change it to:

    RequestQueue productRequestQueue = Volley.newRequestQueue(getApplication());