【问题标题】:Send JSON Object in POST request from Android Volley receive java restful webservices在来自 Android Volley 的 POST 请求中发送 JSON 对象接收 java restful webservices
【发布时间】:2015-12-05 00:48:19
【问题描述】:

我想要一个小例子来发送来自 android volley 的 POST 请求中的 JSON 对象,它必须接收 Java Restful Webservices

【问题讨论】:

    标签: java json rest post android-volley


    【解决方案1】:

    您可以使用以下工作示例代码。希望这会有所帮助!

            ...   
            try {
                RequestQueue queue = Volley.newRequestQueue(this);
                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;
                        }
                    }
                };
                queue.addToRequestQueue(stringRequest);
            } catch (JSONException e) {
                e.printStackTrace();
            }
    

    另外,你能澄清一下it has to receive Java Restful Webservices 是什么意思吗?我的上述请求收到 String 值。

    【讨论】:

    • 我们在 android volley 中通过 POST 请求发送 json 对象我想在 java 中的 restful webservices 中获取相同的 json 对象
    • 那么,试试我的代码,就是以 JSONObject 为主体的 POST 请求
    • @venkatasivaiah:我的回答对你的问题有用吗?
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多