使用POST方法将JSONObject发送到服务器

使用POST方法将JSONObject发送到服务器,post,android-volley,jsonobject,Post,Android Volley,Jsonobject,我想使用volley库将以下格式的jsonobject发送到服务器 { “用户id”:12, “答案”:{ "11": 3, "12": 4, "13": 5 } } JSONObject object = new JSONObject(); try { object.put("user_id", user_id); JSONObject answers = new JSONObject();

我想使用volley库将以下格式的jsonobject发送到服务器 { “用户id”:12, “答案”:{ "11": 3, "12": 4, "13": 5 } }

JSONObject object = new JSONObject();

            try {
                object.put("user_id", user_id);
                JSONObject answers = new JSONObject();
               for (int i = 0; i < questions.size(); i++) {
                    JSONObject answer = new JSONObject();
                    answer.put(questions.get(i).getId(),questions.get(i).getAnswer());
                    answers.put("answers", answer);
                    object.put("answers", answer);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
JSONObject object=new JSONObject();
试一试{
put(“用户id”,用户id);
JSONObject answers=新的JSONObject();
对于(int i=0;i

如果我想使用StringRequest如何使用POST方法将此JsonObject发送到服务器

您可以使用以下工作示例代码。我已经测试过了。希望这有帮助

       try {
            jsonBody = new JSONObject();
            jsonBody.put("Title", "VolleyApp Android Demo");
            jsonBody.put("Author", "BNK");
            jsonBody.put("Date", "2015/08/26");
            requestBody = jsonBody.toString();

            StringRequest stringRequest = new StringRequest(1, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    textView.setText(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    textView.setText(error.toString());
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return String.format("application/json; charset=utf-8");
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return requestBody == null ? null : requestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                                requestBody, "utf-8");
                        return null;
                    }
                }
            };
            MySingleton.getInstance(this).addToRequestQueue(stringRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }

使用以下示例代码

RequestQueue queue = Volley.newRequestQueue(this);

private void serverFronJsonObjReq() {
showProgressDialog();


        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("username", "singh@gmail.com");
        postParam.put("password", "123456");


JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        Const.BASE_URL_LOGIN, new JSONObject(postParam),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                msgResponse.setText(response.toString());
                hideProgressDialog();
            }
        }, new Response.ErrorListener() {

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

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



};

jsonObjReq.setTag(TAG);
// Adding request to request queue
queue.add(jsonObjReq);

// Cancelling request
/* if (queue!= null) {
queue.cancelAll(TAG);
} */

}
RequestQueue queue=Volley.newRequestQueue(this);
私有void服务器fronjsonobjreq(){
showProgressDialog();
Map postParam=新的HashMap();
postParam.put(“用户名”singh@gmail.com");
posparam.put(“密码”、“123456”);
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
Const.BASE\u URL\u登录,新JSONObject(postParam),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
msgressponse.setText(response.toString());
hideProgressDialog();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
hideProgressDialog();
}
}) {
/**
传递一些请求头
* */
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
返回标题;
}
};
jsonObjReq.setTag(TAG);
//将请求添加到请求队列
add(jsonObjReq);
//取消请求
/*if(队列!=null){
queue.cancelAll(标记);
} */
}

感谢您的回答,但如何创建此json格式{“user_id”:12,“answers”:{“11”:3,“12”:4,“13”:5}。
RequestQueue queue = Volley.newRequestQueue(this);

private void serverFronJsonObjReq() {
showProgressDialog();


        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("username", "singh@gmail.com");
        postParam.put("password", "123456");


JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        Const.BASE_URL_LOGIN, new JSONObject(postParam),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                msgResponse.setText(response.toString());
                hideProgressDialog();
            }
        }, new Response.ErrorListener() {

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

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



};

jsonObjReq.setTag(TAG);
// Adding request to request queue
queue.add(jsonObjReq);

// Cancelling request
/* if (queue!= null) {
queue.cancelAll(TAG);
} */

}