如何在android volley中发送简单的原始数组

时间:2016-05-07 07:26:39

标签: android android-volley

任何人都知道如何在android volley中发送带有原始数据的帖子请求..? 我想发布带有标题的数组列表。

[{
    "Name": "qwertytestinggfgfgf"
}]

这是我的代码:

@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部分运作良好。

4 个答案:

答案 0 :(得分:0)

向Hashmap添加更多内容

试试这个,

@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;
    }

这可能对你有所帮助。

答案 1 :(得分:0)

你最好用json发送请求:

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;
    } 
};

答案 2 :(得分:0)

请尝试这个并告诉我:

@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;
}

答案 3 :(得分:0)

在jsonObjectRequest方法中将getBodyContentType重写为jsonBody

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

然后用key键将原始数据放在json对象中。 根对象应该是jsonObject来post.It不应该是json数组

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