Volley的Post方法用于JSON对象

7
data={
    "request": {
       "type": "event_and_offer",
       "devicetype": "A"
    },
    "requestinfo": {
       "value": "offer"
      }
}

请帮忙解释如何使用Volley发布请求。
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.POST,url, null   ,
            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/x-www-form-urlencoded");
            return headers;
        }

JS是指我的JSON对象......我制作我的JSON如下:

JSONObject jsonobject_one = new JSONObject();
    try {
        jsonobject_one.put("type", "event_and_offer");
        jsonobject_one.put("devicetype", "I");

        JSONObject jsonobject_TWO = new JSONObject();
        jsonobject_TWO.put("value", "event");
        jsonobject = new JSONObject();

        jsonobject.put("requestinfo", jsonobject_TWO);
        jsonobject.put("request", jsonobject_one);

        js = new JSONObject();
        js.put("data", jsonobject.toString());
        Log.e("created jsson", "" + js);

但它没有响应值,怎么办?请帮忙。
1个回答

22

首先是你的JSON数据:

JSONObject js = new JSONObject();
try {
    JSONObject jsonobject_one = new JSONObject();

    jsonobject_one.put("type", "event_and_offer");
    jsonobject_one.put("devicetype", "I");

    JSONObject jsonobject_TWO = new JSONObject();
    jsonobject_TWO.put("value", "event");
    JSONObject jsonobject = new JSONObject();

    jsonobject.put("requestinfo", jsonobject_TWO);
    jsonobject.put("request", jsonobject_one);


    js.put("data", jsonobject.toString());

}catch (JSONException e) {
        e.printStackTrace();
}

那么您的JSON请求:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(
        Request.Method.POST,url, js,
        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;
    }

注意标题

如果你想在本地测试,请使用以下代码并将您的URL设置为连接您的本地主机服务器和IP地址: 以下代码将所有请求放入一个文本文件中,我尝试过它可以工作

<?php
file_put_contents('test.txt', file_get_contents('php://input'));
?>

2
为什么需要发送 charset=utf-8 而不仅仅是 'application/json' - Ujju

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