如何使用Volley提交布尔值或整数值

5
我希望使用Volley库发布布尔型和双精度数据。我不知道该如何使用它。是否有其他方法?提前感谢。
这是我的方法...
@Override

        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<String, String>();
            params.put("name", "name");
            params.put("email", "abc@abc.info");
            params.put("pass", "password");

            return params;
        }
4个回答

5
JSONObject obj = new JSONObject();

obj.put("isboolean",false)

JsonObjectRequest req = new JsonObjectRequest(Constants.URL_PATH, obj,
                new Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject response) {


}, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

2
这个方法的类型放在哪里? - John
谢谢,问题已解决,我没有从API那里得到任何结果。 - Joseph

2
JSONObject object = new JSONObject();
try {
    object.put("compression", false);
    object.put("instructions", true);
} catch (JSONException e) {
    e.printStackTrace();
}

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, API_URL, object, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        parseDirectionsData(response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
}){
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        HashMap<String, String> params = new HashMap<>();
        params.put("loc", "-33.9717974,18.6029783");
        return params;
    }
};

任���不是字符串的参数都可以包装成Json对象。

1
这是一个Volley调用的完整请求。 您可以更改方法调用。 您可以将任何类型的参数作为JSON对象传递。 您可以设置请求头。
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("stringValue", "abc");
            jsonObject.put("doubleValue", 13.066);
            jsonObject.put("integerValue", 120);
            jsonObject.put("booleanValue", true);

            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getString(R.string.api_url), jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            getJsonResult(response);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }
            ) {
                @Override       //Send Header
                public Map<String, String> getHeaders() throws AuthFailureError {

                    Map<String, String> params = new HashMap<>();
                    params.put("api_call_header", "header_value");

                    return params;
                }
            };
            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(request);`enter code here`

0
如果您有自定义请求,只需覆盖getBody方法即可开始工作:
@Override
public byte[] getBody(){
        JSONObject jsonObject = new JSONObject();
        String body = null;
        try{
            //Here are your parameters:
            jsonObject.put("name", "geronimous");
            jsonObject.put("age", 998);
            jsonObject.put("happy", true);

            body = jsonObject.toString();
        } catch (JSONException e){
            e.printStackTrace();
        }
        try{
            return body.toString().getBytes("utf-8");
        } catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return null;

}

请确保您在标头中设置内容类型为application/json


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接