我如何发送JsonObject并重新发送JsonArray

时间:2015-06-17 10:08:45

标签: android json android-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);
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法将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了解详情。

答案 1 :(得分:-1)

public class nameOfClass extends 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();
        }

    }
}