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

时间:2015-11-28 11:00:26

标签: android android-volley android-5.0-lollipop android-4.4-kitkat

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-2)

试试这个,

将Request.Method.PATCH更改为Request.Method.POST,然后将标题类型更改为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:这只是一个黑客,但不是实际的解决方案。