Ravi Ravi - 1 month ago 5
JSON Question

How to post JSON data using volley library?

I have following format of posting data to server. I want to post these data to server using volley library and following request contains multiple images' base64 content also. I am passing this as StringRequest i got the error of invalid JSON data format. How can i post following data to server? Or any other useful way to pass following data to server then also let me know. So that i can solve this problem and efficiently can upload on server.

{
"TakeoffID": "2",
"ViewPhoto1": "image base64 content",
"ViewPhoto2": "image base64 content",
"LineItems": [
{
"OrderLineid": "964",
"OrderLinePhoto1": "image base64 content",
"OrderLinePhoto2": "image base64 content"
},
{
"OrderLineid": "963",
"OrderLinePhoto1": "image base64 content",
"OrderLinePhoto2": "image base64 content"
}
]
}


===========
Following is my code to upload above data:

private void uploadImage(final CustomerBean bean,final String line_items)
{
// Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(mActivity, "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, Const.API_SYNC_ALL_DATA, new Response.Listener<String>() {
@Override
public void onResponse(String s)
{

loading.dismiss();
Log.print("======UPLOAD IMAGE=====", s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError)
{
loading.dismiss();
Toast.makeText(mActivity, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError
{


Map<String, String> params = new Hashtable<String, String>();

params.put("TakeoffID", "2");
params.put("ViewPhoto1", bean.photo1);
params.put("ViewPhoto2", bean.photo2);
params.put("LineItems", line_items);
// returning parameters
return params;
}
};

// Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(mActivity);

// Adding request to the queue
requestQueue.add(stringRequest);
}


===========================

And I am getting following error from server.

[
{
"code": "-1",
"message": "The json data format is incorrect"
}
]

BNK BNK
Answer

IMO, you can refer to my following sample code:

        try {
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            String URL = "http://...";
            // Prepares POST data...
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("TakeoffID", "2");
            jsonBody.put("ViewPhoto1", "image base64 content");
            jsonBody.put("ViewPhoto2", "image base64 content");
            // "OrderLineid": "964"...
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("OrderLineid","964");
            jsonObject1.put("OrderLinePhoto1","image base64 content");
            jsonObject1.put("OrderLinePhoto2","image base64 content");
            // "OrderLineid": "963"...
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("OrderLineid","963");
            jsonObject2.put("OrderLinePhoto1","image base64 content");
            jsonObject2.put("OrderLinePhoto2","image base64 content");
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(jsonObject1);
            jsonArray.put(jsonObject2);
            // "LineItems"...
            jsonBody.put("LineItems", jsonArray);
            final String mRequestBody = jsonBody.toString();
            // Volley request...
            StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("VOLLEY", response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY", error.toString());
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }
                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                                mRequestBody, "utf-8");
                        return null;
                    }
                }
            };
            requestQueue.add(request);
        } catch (JSONException e) {
            e.printStackTrace();
        }

Hope it helps!