Json对象的Volley Post方法

时间:2022-02-11 21:31:33
data={
    "request": {
       "type": "event_and_offer",
       "devicetype": "A"
    },
    "requestinfo": {
       "value": "offer"
      }
}

how to post this request from volley plz help

如何从volley plz帮助发布此请求

            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 is reffer to my jsson object ...... i make my jsson like this.....

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

But it Not response the value what to do plzz help

但它没有回应plzz帮助的价值

1 个解决方案

#1


18  

first your json data:

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

then your json request:

然后你的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;
    }

Notice the header

注意标题

and if you want to test in localhost use below code and set your url to connect your localhost server and ip address: below code put all of your request in a text file, i tried it and it works

如果你想在localhost中测试使用下面的代码,并设置你的网址连接你的本地主机服务器和IP地址:下面的代码将你的所有请求放在一个文本文件中,我尝试了它,它的工作原理

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

#1


18  

first your json data:

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

then your json request:

然后你的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;
    }

Notice the header

注意标题

and if you want to test in localhost use below code and set your url to connect your localhost server and ip address: below code put all of your request in a text file, i tried it and it works

如果你想在localhost中测试使用下面的代码,并设置你的网址连接你的本地主机服务器和IP地址:下面的代码将你的所有请求放在一个文本文件中,我尝试了它,它的工作原理

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