如何使用齐射请求在嵌套的json对象内发送字节数组?

时间:2018-12-29 21:23:43

标签: android android-volley jsonobjectrequest

我有一个json对象,其中包含有关评估表单的详细信息以及其中的json数组,其中包含有关评估问题的信息(例如questionId,答案说明,用户上传的某些问题的图片)。我想将此评估信息发送到带有排球库的服务器。为此,我准备了一个json对象,如下所示:

{"evaluation":{"UserID":1,"EvaluationDate":"2018\/29\/12","EvaluationDetailsList":
[{"QuestionID":1,"Description":null,"Image":null,"PercentScore":0,"QuestionResponseID":3},
"QuestionID":2,"Description":null,"Image":null,"PercentScore":0,"QuestionResponseID":3},
"QuestionID":3,"Description":null,"Image":null,"PercentScore":0,"QuestionResponseID":3},
{"QuestionID":4,"Description":null,"Image":"B@927f5e6","PercentScore":0,"QuestionResponseID":3}]}}

EvaluationDetailsList是我关于评估问题的Json数组。当我使用空的Image字段发送发布请求时,代码可以正常工作,但是当我在Image字段中放置字节数组时出现错误。我用谷歌搜索如何用volley将字节数组发送到Web服务器。答案是使用凌空抽射的MultiPartRequest。但是当Image字段位于嵌套的json对象内部时,我不知道如何使用此请求方法。这是我向服务器发送请求:

public void sendEvalResult(final JSONObject jsonObject, final ResultCallBack resultCallBack){
        JSONObject finalObject=new JSONObject();
        try {
            finalObject.put("evaluation",jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest req=new JsonObjectRequest(Request.Method.POST, Post_Evaluation_Result, finalObject,new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                resultCallBack.onGetResult(null);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                resultCallBack.onFailedGet();
            }
        }){
            @Override
            public String getBodyContentType() {
                return "application/json;";
            }
        };

        AppSingleton.getInstance(context).addToRequestQueue(req,"PostResult");}

请帮助

0 个答案:

没有答案