如何使用齐射库发布JSON数据?

时间:2015-12-11 09:15:18

标签: android json android-volley

我有以下格式将数据发布到服务器。我想使用排球库将这些数据发布到服务器,并且以下请求包含多个图像' base64内容也。我传递这个作为StringRequest我得到了无效的JSON数据格式的错误。如何将以下数据发布到服务器?或者将以下数据传递给服务器的任何其他有用方法然后也让我知道。这样我就可以解决这个问题,并且可以有效地上传到服务器上。

{
    "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"
        }
    ]
}

=========== 以下是我上传数据的代码:

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"
    }
]

2 个答案:

答案 0 :(得分:2)

您还可以使用以下代码块将pass json对象传递给Volley作为参数。只需检查一次。

JSONObject data= new JSONObject();

            data.accumulate("username", "mobileGps");
            data.accumulate("password", "9565551236");

            JSONObject total= new JSONObject();
            total.put("data",data);

            json = jsonObjectNew.toString(); 

答案 1 :(得分:0)

IMO,您可以参考我的以下示例代码:

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

希望它有所帮助!