将Volley中的JsonObject作为POST参数传递

时间:2015-09-07 11:35:34

标签: android

我有一个API端点,需要在POST请求中将参数作为JsonObject发送。

我将得到的回应不是Json,而是一个小的CSV字符串。

守则

stringRequest = new StringRequest(Request.Method.GET, "http://apitest.eezyrent.com/api/userauthentication/SignUp",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Toast.makeText(getApplication(), response.toString(), Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplication(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> jsonParams2 = new HashMap<String, String>();
                jsonParams2.put("vcFName", firstname.getText().toString());
                jsonParams2.put("vcLname", lastname.getText().toString());
                jsonParams2.put("vcMobileNo", phone_no.getText().toString());
                jsonParams2.put("vcGender", gender_short);
                jsonParams2.put("vcEmailAddress", email.getText().toString());
                jsonParams2.put("vcPassword", password.getText().toString());
                jsonParams2.put("vcFBID", "");
                jsonParams2.put("intLoginUserID", "");
                jsonParams2.put("SignUpFrom", "Web");
                jsonParams2.put("intloginid", "");
                jsonParams2.put("AlreadyRegister", "");
                return jsonParams2;
            }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put( "charset", "utf-8");
            return headers;
        }
    };

上述代码的灵感来自于使用此社区的答案,但这似乎并没有为我解决问题。

我得到Volley Error 405。

 E/Volley﹕ [112549] BasicNetwork.performRequest: Unexpected response code 405 for http://myurl

Infact如果我使用AsyncTask而不是Volley,使用与参数相同的json和相同的端点url。有用!! AsyncTask代码是从这个问题中找到的。 Java HttpClient changing content-type?

但我想使用Volley,这可能是什么解决方案呢?

示例JSON对象

{"vcFName":"Ron","vcLname":"Weasley","vcMobileNo":"555888999","vcGender":"M","vcEmailAddress":"someone@somewhere.com","vcPassword":"123456","vcFBID":"","intLoginUserID":'',"SignUpFrom":"Web","intloginid":"","AlreadyRegister":""}

2 个答案:

答案 0 :(得分:0)

您需要使用JsonObjectRequest类。将params作为json对象传递给JsonObjectRequest类的第3个参数。下面是一个小片段:

 Map<String, String> jsonParams2 = new HashMap<String, String>();
            jsonParams2.put("vcFName", firstname.getText().toString());
            jsonParams2.put("vcLname", lastname.getText().toString());
            jsonParams2.put("vcMobileNo", phone_no.getText().toString());
            jsonParams2.put("vcGender", gender_short);
            jsonParams2.put("vcEmailAddress", email.getText().toString());
            jsonParams2.put("vcPassword", password.getText().toString());
            jsonParams2.put("vcFBID", "");
            jsonParams2.put("intLoginUserID", "");
            jsonParams2.put("SignUpFrom", "Web");
            jsonParams2.put("intloginid", "");
            jsonParams2.put("AlreadyRegister", "");

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        YourUrl, new JsonObject(jsonParams2),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put( "charset", "utf-8");
        return headers;
    }
};

答案 1 :(得分:0)

步骤1:使模型类可解析/可序列化 第2步:在模型类中覆盖toString() 第3步:      Map<String,JSONObject> params = new HashMap<>(); JSONObject object = null; try{ object = new JSONObject(classObject.toString()); }catch (Exception e){ } params.put("key", object); JSONObject objectParams = new JSONObject(params);
第4步:使用volley JSONObject请求发送objectParams 完成!!!