2

我正在将联系人添加到活动的活动 api,但请求没有发送帖子参数。参数是从邮递员发送的,但凌空它不起作用。我也尝试从构造函数发送参数,但没有进展。这是代码。

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);
4

2 回答 2

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);
于 2018-01-05T06:45:05.150 回答
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");
于 2018-01-05T06:45:35.627 回答