Volley PATCH 在 Pre-lollipop deives 中不起作用

Volley PATCH is not working in Pre-lollipop deives

volley + gson PATCH 在 pre-lollipo [Kitkat] 设备中不起作用。 这是代码.....

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());
                                   }
                               });

试试这个,

将 Request.Method.PATCH 更改为 Request.Method.POST,然后将 header 类型更改为 PUT,如下所示。

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:这只是一个 hack,而不是实际的解决方案。

在发送请求时使用 POST 并将 X-HTTP-Method-Override 覆盖到 PATCH

headers.put("X-HTTP-Method-Override", "PATCH");