简体   繁体   中英

how to send simple raw array in android volley

Anyone know how to send post request with raw data in android volley ..? I want to post this array list with headers.

[{
    "Name": "qwertytestinggfgfgf"
}]

Here is my code:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json");
    headers.put("Authorization", "my Authorization code is here ");
    return headers;
}

@Override
protected Map<String, String> getParams() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("name", "rahatamjid");
    return params;
}

Here is a screen shot.

headesr section is working good.

Add more to Hashmap

Try like this,

@Override
    protected Map<String, String> getParams() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("name1", "rahatamjid1");
        params.put("name2", "rahatamjid2");
        params.put("name3", "rahatamjid3");
        params.put("name4", "rahatamjid4");

        Log.i("request","" + params);
        return params;
    }

This may helps you.

You better send request with json like this:

JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST,httpurl, jsonObject,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "response -> " + response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, error.getMessage(), error);
    }
    })
    {  
    @Override 
    public Map<String,String> getHeaders() throws AuthFailureError {
        Map<String,String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "my Authorization code is here ");
        return headers;
    } 
};

please try this and let me know:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
  HashMap<String, String> params = new HashMap<String, String>();
  String creds = String.format("%s:%s","USERNAME","PASSWORD");  //please adapt this to your auth type
  String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
  params.put("Authorization", auth);
  params.put("Content-Type", "application/json");// 
  return params;
}

Override getBodyContentType to jsonBody in jsonObjectRequest method

 @Override
            public String getBodyContentType() {
                return "application/json;charset=UTF-8";
            }

then put your raw data in json object with key. the root object should be jsonObject to post.It should not be json array

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("yourKey",value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM