我如何发送 Json 对象并接收 JsonArray

How I send a JsonObject and recive a JsonArray

当我测试 "volley" 库时,我对方法 "post" 产生了疑问。

问题是我一直在使用 JsonObject 直到现在,为此我使用了以下代码并且它有效。在这种方法中,我发送 JsonObject 并接收另一个 JsonObjet,我没有遇到任何问题。

现在我的疑问是,我该如何发送一个 JsonObject 并接收一个 ArrayObject?

我真的很迷茫,非常感谢你的帮助,在此先感谢

public void testPost(String url, final String tag, JSONObject obj) {
    //final ProgressDialog pDialog = new ProgressDialog(context);
    //pDialog.setMessage("Loading...");
    //pDialog.show();
    JsonObjectRequest jsonObjReqpost = new JsonObjectRequest(Request.Method.POST,
            url, obj,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    //test
                    Log.d("SendRequestJsonPost", response.toString());
                    //pDialog.hide();
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (error.networkResponse != null && error.networkResponse.data != null) {
                error = new VolleyError(new String(error.networkResponse.data));
            }
            String fail = handlingErrors.resolverErrors(error.toString());
            //Log.d("SendRequestJsonPost", fail);
            //pDialog.hide();
        }
    }) {
        //header
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("charset", "utf-8");
            return headers;
        }
    };
    jsonObjReqpost.setTag(tag);
    Singleton.getInstance(context).addToRequestQueue(jsonObjReqpost);
}

public class nameOfClass 扩展 AsyncTask { 字符串响应;

//This process is work in Background
    @Override
    protected String doInBackground(String... params) {
        SocketConnection connection = new SocketConnection();
        String token;

        //Tocken name that is send to server
        token = "TockenName|" + "|";

        try {

            response = connection.EstablishConnection(token);

            String message = "Sorry Fail to connect";
            if (response.equals(message)) {
                onPostExecute(message);
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
// In this method you can open loading msg
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        try {
            //Json object
            JSONArray array = new JSONArray(result);
            for (int i = 0; i < array.length(); i++) {

                //Hear traverse the loop and get the data

             }  
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

    }
}

您可以使用以下方法获取 JsonArray 作为响应。

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONArray> listener, ErrorListener errorListener) 
{
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
        listener, errorListener);
}

详情请参考this answer