How to send request Header is “Content-Type”:“application/json” when GET on Volley

后端 未结 4 1174
梦毁少年i
梦毁少年i 2021-01-18 01:02

I try to use GET on Volley , but i need send request to application/json .

I take a look for some answers , i try to use jsonBody , but it

4条回答
  •  野的像风
    2021-01-18 01:12

    I try to use GET on Volley

    The docs for the method you are calling says this

    Constructor which defaults to GET if jsonRequest is null, POST otherwise

    You can't GET with an HTTP JSON body. Maybe that's the error.

    //I try to use this for send Header is application/json
    jsonBody = new JSONObject("{\"type\":\"example\"}");
    

    That's not the header, so pass in null here to do GET

    new JsonObjectRequest(url, null,
    

    And towards the end of your request, override a method to request JSON

            ... 
    
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
    
    
        }) { // Notice no semi-colon here
            @Override 
            public Map getHeaders() throws AuthFailureError { 
                Map params = new HashMap();                
                params.put("Content-Type", "application/json");
                return params; 
            } 
        };
    

提交回复
热议问题