Android: Volley HTTP Request custom header

前端 未结 3 961
有刺的猬
有刺的猬 2020-11-27 22:09

I\'m getting the following error when I run the app: BasicNetwork.performRequest: Unexpected response code 401

I need to pass an email, a password and a token to acc

相关标签:
3条回答
  • 2020-11-27 22:18

    this one worked for me (seen on Volley JsonObjectRequest Post parameters no longer work)

    String url = "https://www.youraddress.com/";
    
    Map<String, String> params = new HashMap();
    params.put("first_param", 1);
    params.put("second_param", 2);
    
    JSONObject parameters = new JSONObject(params);
    
    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, parameters, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //TODO: handle success
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            //TODO: handle failure
        }
    });
    
    Volley.newRequestQueue(this).add(jsonRequest);
    
    0 讨论(0)
  • 2020-11-27 22:35

    The JsonObjectRequest is extended JsonRequest which override getBody() method directly, so your getParam() would never invoke, I recommend you extend StringRequest instead of JsonObjectRequest.

    your can check this answer for more details.

    by the way, you've another choice : Netroid, that based Volley, offered more features at all.

    0 讨论(0)
  • 2020-11-27 22:38

    If you want to send your Map as a Json object (you are using the class JsonObjectRequest), you should put it in the JsonobjectRequest constructor :

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("email", "rm@test.com.br");
    params.put("senha", "test");
    params.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0afd19MfacGf3FFm8CM1hG0eDiIk8");
    
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(url, new JSONObject(params), ..., ...);
    

    See this tuto

    0 讨论(0)
提交回复
热议问题