在凌空一个JSONObject作为POST参数 [英] Pass a JsonObject in Volley as POST parameter

查看:474
本文介绍了在凌空一个JSONObject作为POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要参数的API端点作为JSONObject的POST请求被发送。

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

在code是

  stringRequest =新StringRequest(Request.Method.GET,http://apitest.eezyrent.com/api/userauthentication/SignUp
                新Response.Listener<串GT;(){
                    @覆盖
                    公共无效onResponse(字符串响应){
                        //显示响应字符串的前500个字符。
                        Toast.makeText(getApplication(),response.toString(),Toast.LENGTH_SHORT).show();
                    }
                },新Response.ErrorListener(){
            @覆盖
            公共无效onErrorResponse(VolleyError错误){
                Toast.makeText(getApplication(),error.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }){            @覆盖
            保护地图<字符串,字符串> getParams()方法抛出AuthFailureError {
                地图<字符串,字符串> jsonParams2 =新的HashMap<字符串,字符串>();
                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,网页);
                jsonParams2.put(intloginid,);
                jsonParams2.put(AlreadyRegister,);
                返回jsonParams2;
            }        @覆盖
        公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
            HashMap的<字符串,字符串>标题=新的HashMap<字符串,字符串>();
            headers.put(内容类型,应用/ JSON);
            headers.put(字符集,UTF-8);
            返回头;
        }
    };

以上code是大量使用从解答这个社区的启发,但是这似乎并没有解决我的问题。

我得到凌空错误405。

  E /乱射:[112549] BasicNetwork.performRequest:意外的响应code 405的http:// myurl

逸岸如果我用的AsyncTask代替凌空,与同JSON作为参数和相同的端点URL。有用!!该AsyncTask的code从这个问题中。 的Java HttpClient的变化的内容类型?

不过,我想用排枪,还有什么能解决这个?

样JSON对象

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


解决方案

您需要使用JsonObjectRequest类。通过您的PARAMS作为JsonObjectRequest类的第3个参数JSON对象。下面是一个小片段:

 地图&LT;字符串,字符串&GT; jsonParams2 =新的HashMap&LT;字符串,字符串&GT;();
            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,网页);
            jsonParams2.put(intloginid,);
            jsonParams2.put(AlreadyRegister,);JsonObjectRequest jsonObjReq =新JsonObjectRequest(Method.POST,
        YourUrl,新的JSONObject(jsonParams2)
        新Response.Listener&LT;&JSONObject的GT;(){            @覆盖
            公共无效onResponse(JSONObject的响应){            }
        },新Response.ErrorListener(){            @覆盖
            公共无效onErrorResponse(VolleyError错误){            }
        }){
    @覆盖
    公共地图&LT;字符串,字符串&GT; getHeaders()抛出AuthFailureError {
        HashMap的&LT;字符串,字符串&GT;标题=新的HashMap&LT;字符串,字符串&GT;();
        headers.put(内容类型,应用/ JSON);
        headers.put(字符集,UTF-8);
        返回头;
    }
};

I have an API endpoint which needs parameters to be sent as JsonObject in a POST request.

The response which I will get is not Json, but rather a small CSV string.

The Code is

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

The above code is heavily inspired by using answers from this community, but this does not seem to solve the problem for me.

I get Volley Error 405.

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

Infact If I used AsyncTask instead of Volley, With the same json as parameters and the same endpoint url. It works!! The AsyncTask code was found from this question. Java HttpClient changing content-type?

But I want to use Volley, What could be the solution to this?

Sample JSON Object

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

解决方案

You need to use JsonObjectRequest class. Pass your params as json object in the 3rd parameter of the JsonObjectRequest class. Below is a small snippet:

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

这篇关于在凌空一个JSONObject作为POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆