Search code examples
androidandroid-volleyandroid-5.0-lollipopandroid-4.4-kitkat

Volley PATCH is not working in Pre-lollipop deives


volley + gson PATCH is not working in pre-lollipo [Kitkat] devices. Here is the code.....

GsonRequestJson gsonRequestJson = new GsonRequestJson(Request.Method.PATCH,url, WishlistItems.class, headers, jsonObject, new Response.Listener<WishlistItems>() {
                                       @Override
                                       public void onResponse(WishlistItems response) {
                                           Toast.makeText(mContext, "Successfully removed from wishlist", Toast.LENGTH_SHORT).show();
                                           hEart.setBackgroundResource(R.drawable.ic_action_heartempty);
                                           // dialog.dismiss();

                                       Gson gson = new Gson();
                                       String jsonFavorites = gson.toJson(new ArrayList<>(Arrays.asList(response)));
                                       MainActivity.tinyDB.putString(Constants.MY_SHARED_PREF_MARKEDITEMS, jsonFavorites);

                                   }
                               }, new Response.ErrorListener() {
                                   @Override
                                   public void onErrorResponse(VolleyError error) {
                                       Toast.makeText(mContext, "Failed", Toast.LENGTH_LONG)
                                               .show();
                                       Log.d("Error---", "-----" + error.toString());
                                   }
                               });

Solution

  • Try this,

    Change Request.Method.PATCH to Request.Method.POST, and then change the header type to PUT as below.

    HashMap<String, String> headers = new HashMap<String, String>();
                                headers.put("Authorization", "JWT " + Constants.load(mContext,Constants.MY_SHARED_PREF_TOKEN));
                                headers.put("Content-Type", "application/json");
                                headers.put("X-HTTP-Method-Override", "PUT");
    

    PS: This is just a hack but not the actual solution.