【问题标题】:Volley Post method for json objectjson对象的Volley Post方法
【发布时间】:2014-11-12 10:35:22
【问题描述】:
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 引用我的 jsson 对象......我让我的 jsson 像这样......

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

但它没有响应值怎么办请帮助

【问题讨论】:

    标签: android post android-volley jsonobject


    【解决方案1】:

    首先是你的 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;
        }
    

    注意标题

    如果您想在 localhost 中进行测试,请使用以下代码并设置您的 url 以连接您的 localhost 服务器和 IP 地址: 下面的代码将您的所有请求放在一个文本文件中,我试过了,它可以工作

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

    【讨论】:

    • 为什么需要发送charset=utf-8 而不仅仅是'application/json'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2017-09-18
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多