Volley Post method for json object

前端 未结 1 1237
data={
    \"request\": {
       \"type\": \"event_and_offer\",
       \"devicetype\": \"A\"
    },
    \"requestinfo\": {
       \"value\": \"offer\"
      }
}


        
相关标签:
1条回答
  • 2020-12-09 01:20

    first your json data:

    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:

    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

    <?php
    file_put_contents('test.txt', file_get_contents('php://input'));
    ?>
    
    0 讨论(0)
提交回复
热议问题