简体   繁体   中英

How to create a post request in volley android with parameters

I am building an android application that should communicates with the server using volley, it sends the server a request with data in it that should be processed on server and results, sent back. I am always getting an error from the server, and It's most probably because I couldn't build the request right. This is the format of the request :

POST API/{RESOURCE}
Content-type: application/json

{
"documents" :
    [
        { "image" : "<base64-encoded image>", "type": "id_front  or id_back" },
        { "image" : "<base64-encoded image>", "type": "id_front  or id_back" }
                             … 
    ],

    "token" : "<access_token_string>" 
}

This is how i tried to achieve it, is it right ? I am not sure if the problem is with this code but it's most probable that it's in it.

  JSONObject parameters = new JSONObject();

        JSONObject[] jsonObjects;
        jsonObjects = new JSONObject[1];
        try {
            Map<String , String> im = new HashMap<>();
            im.put("image",imageStringBase64);
            im.put("type","id_front");
            jsonObjects[0] = new JSONObject(im);

               parameters = parameters.put("token", "****");
           parameters= parameters.put("documents", jsonObjects);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, API_URL, parameters, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                StringBuilder formattedResult = new StringBuilder();
                formattedResult.append(response);
                Log.d(TAG, "jsonObjectResponse : " + response.toString());

            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "ErrorResponse called " + "error code: " + error.networkResponse.statusCode);
            }
        })

        {

            /** Passing some request headers* */
            @Override
            public Map getHeaders() throws AuthFailureError {
                HashMap headers = new HashMap();
                headers.put("Content-Type", "application/json");
                return headers;
            }
        };

        mQueue.add(jsonObjectRequest);

Try building your request like below:

JSONObject requestObj;
try {
    requestObj = new JSONObject();
    JSONArray arr = new JSONArray();

    JSONObject obj1 = new JSONObject();
    obj1.put("image", imageStringBase64);
    obj1.put("type","id_front");
    JSONObject obj2 = new JSONObject();
    obj2.put("image", imageStringBase64);
    obj2.put("type","id_back");
    arr.put(obj1);
    arr.put(obj2);

    requestObj.put("documents", arr);
    requestObj.put("token", "****");
} catch (JSONException e) {
    e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, requestObj,
        listener, errorListener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM