使用JsonObjectRequest volley将post参数发送到活动的campaign api

时间:2018-01-05 06:37:10

标签: android android-volley postman jsonobjectrequest post-parameter

我正在向活跃的广告系列API添加联系人,但请求没有发送帖子参数。参数是从邮递员发送的,但是凌空不起作用。我试过从构造函数发送params但是没有进展。这是代码。

Map<String, String> params = new HashMap();
params.put("email", "wff@dd.com");
params.put("p[1]", "1");
//JSONObject parameters = new JSONObject(params);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
url="https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json";
Log.d("url",url);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Log.d("url",response.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub
        Log.d("url",error.toString());
        error.printStackTrace();
    }
}){
    @Override
    public byte[] getBody() {
        HashMap<String, String> params2 = new HashMap<String, String>();
        params2.put("email", "w@sss.com");
        params2.put("p[1]", "1");
        return new JSONObject(params2).toString().getBytes();
    }

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded;";
        }

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params2 = new HashMap<String, String>();
            params2.put("email", "w@sss.com");
            params2.put("p[1]", "1");
            return params2;
        }
    }
};
queue.add(jsObjRequest);

2 个答案:

答案 0 :(得分:0)

使用StringRequest

尝试此
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
       "https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json",
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("url",response.toString());

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("volley", "Error: " + error.getMessage());

            }
        }) {

    @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("email", "w@sss.com");
        params.put("p[1]", "1");
        return params;
    }

};

queue.add(jsonObjRequest);

答案 1 :(得分:0)

使用stringRequest这样检查这个

   //Tested on PostMan
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("userName", "cyborg91mv@gmail.com");
            params.put("password", "qwerty");
            System.out.println(params);
            return params;
        }

Now if your server expects json, then make jsonRequest
 JSONObject jsonObjectBody = new JSONObject();
        jsonObjectBody.put("userName", "cyborg91mv@gmail.com");
        jsonObjectBody.put("password", "qwerty");