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

后端 未结 4 1168
梦毁少年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<String, String> getHeaders() throws AuthFailureError { 
                Map<String, String> params = new HashMap<String, String>();                
                params.put("Content-Type", "application/json");
                return params; 
            } 
        };
    
    0 讨论(0)
  • 2021-01-18 01:16
    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>();                
        params.put("Content-Type", "application/json");
        return params; 
    } 
    

    Implementation in your's

    public class MainActivity extends AppCompatActivity {
    
        String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
    
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                    }
                }) { //no semicolon or coma
                @Override 
                public Map<String, String> getHeaders() throws AuthFailureError { 
                    Map<String, String> params = new HashMap<String, String>();                
                    params.put("Content-Type", "application/json");
                    return params; 
                } 
            };
            mQueue.add(jsonObjectRequest);
        }
    }
    
    0 讨论(0)
  • 2021-01-18 01:21

    In general for setting a custom header you need to override getHeaders and set the custom header manually. However, volley handles content type headers differently and getHeaders way does not always work.

    So for your case you need to override getBodyContentType. So your code will look like

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,new Response.Listener<JSONObject>() {
    
    @Override
    public void onResponse(JSONObject response) {
       Log.d("TAG", response.toString());
    }, new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
    
    
        }){
             @Override
             public String getBodyContentType(){
                  return "application/json";
             }
        };
    
    0 讨论(0)
  • 2021-01-18 01:21

    Use String request instead of jsonrequest like this

                StringRequest loginMe = new StringRequest(Request.Method.GET, "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews", new Response.Listener<String>() {
    
                    @Override
                    public void onResponse(String response) {
    
                        System.out.println("LoginActivity -- onResponse --> " + response);
    
                        if (progressDialog != null) {
    
                            progressDialog.dismiss();
    
                        }
    
                        try {
    
                            JSONObject jsonObject = new JSONObject(response);
    
                        } catch (Exception e) {
    
                            CommonUtility.somethingWentWrongDialog(activity,
                                    "LoginActivity -- onResponse-- Exception --> ".concat(e.getMessage()));
    
                        }
                    }
                }, new Response.ErrorListener() {
    
                    @Override
                    public void onErrorResponse(VolleyError error) {
    
                        if (progressDialog != null) {
    
                            progressDialog.dismiss();
    
                        }
    
                        CommonUtility.somethingWentWrongDialog(activity,
                                "LoginActivity -- onErrorResponse --> ".concat(error.getMessage()));
    
                    }
                }) {
    
                    @Override
                    protected Map<String, String> getParams() {
    
                        Map<String, String> params = new HashMap<>();
    
    
                        System.out.println("LoginActivity -- LoginParams --> " + params.toString());
    
                        return params;
    
                    }
                };
    
                loginMe.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
                Volley.newRequestQueue(activity).add(loginMe);
    
    0 讨论(0)
提交回复
热议问题