将参数添加到Volley PUT请求 - Android

时间:2015-08-04 05:06:54

标签: android parameters android-volley put

我一直在我的应用中使用排球库进行网络连接。我能够在GET和POST请求中附加参数,两者都工作正常,但无法弄清楚如何在PUT请求中执行此操作。我总是收到“415- status{ Unsupported media type} -result { Invalid content Type}”错误。做了很多搜索但没有工作。

这就是我试过的

RequestQueue requestQueue = VolleyRequests.getInstance().getRequestQueue();
    String uri = "https://myURI";
    StringRequest request = new StringRequest(Request.Method.PUT, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.d("server response",response);
            userCallBack.done(response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String>  headers = new HashMap<String, String>();
            headers.put("secret_apikey", "xxxxxxx");
            headers.put("Content-Type", "application/json");
            headers.put("Cache-Control", "no-cache");
            headers.put("Accept", "application/json");
            return headers;
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("userid", "013");

            params.put("sale_amount","2000");
            return params;
        }};
   requestQueue.add(request);

我也尝试在字节数组中添加参数。这是代码。

 @Override
 public byte[] getBody() throws AuthFailureError {
            byte[] b = null;
            JSONObject obj = new JSONObject();
            try {
                obj.put("userid", "013");            
                obj.put("sale_amount","2000");
            } catch (JSONException e) {
                Log.d("Excep", String.valueOf(e));
            }
            try {
                Log.i("json", obj.toString());
                b = obj.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                Log.d("Excep", String.valueOf(e));
        }

            return b;
        }

我无法用POST或get请求替换它,因为我调用的API只接受PUT请求。请帮助。

0 个答案:

没有答案