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

后端 未结 4 1173
梦毁少年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:16

    @Override 
    public Map getHeaders() throws AuthFailureError { 
        Map params = new HashMap();                
        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() {
                    @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 getHeaders() throws AuthFailureError { 
                    Map params = new HashMap();                
                    params.put("Content-Type", "application/json");
                    return params; 
                } 
            };
            mQueue.add(jsonObjectRequest);
        }
    }
    

提交回复
热议问题