Android 如何发送JsonObject并接收JsonArray

Android 如何发送JsonObject并接收JsonArray,android,json,android-volley,Android,Json,Android Volley,当我测试“截击”库时,我对“post”方法产生了疑问 问题是,到目前为止,我一直在使用JsonObject,因为我使用了以下代码,它可以正常工作。在这个方法中,我发送和接收另一个JsonObject,我对它没有问题 现在我的疑问是,如何发送JsonObject并接收ArrayObject 我真的很迷茫,我会非常感谢你的帮助,提前谢谢 public void testPost(String url, final String tag, JSONObject obj) { //final P

当我测试“截击”库时,我对“post”方法产生了疑问

问题是,到目前为止,我一直在使用JsonObject,因为我使用了以下代码,它可以正常工作。在这个方法中,我发送和接收另一个JsonObject,我对它没有问题

现在我的疑问是,如何发送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 void testPost(字符串url、最终字符串标记、JSONObject obj){
//final ProgressDialog pDialog=新建ProgressDialog(上下文);
//设置消息(“加载…”);
//pDialog.show();
JsonObjectRequest JSONObjectReqPost=新的JsonObjectRequest(Request.Method.POST,
url,obj,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
//试验
Log.d(“SendRequestJsonPost”,response.toString());
//pDialog.hide();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
if(error.networkResponse!=null&&error.networkResponse.data!=null){
error=新截击错误(新字符串(error.networkResponse.data));
}
String fail=handlingErrors.resolverErrors(error.toString());
//Log.d(“SendRequestJsonPost”,失败);
//pDialog.hide();
}
}) {
//标题
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
标题。put(“字符集”、“utf-8”);
返回标题;
}
};
jsonObjReqpost.setTag(tag);
Singleton.getInstance(context).addToRequestQueue(jsonObjReqpost);
}

类的公共类名称扩展异步任务{ 字符串响应

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

    }
}
//此过程在后台工作
@凌驾
受保护的字符串doInBackground(字符串…参数){
SocketConnection=新的SocketConnection();
字符串标记;
//Tocken发送到服务器的名称
tokenname |“+”|“;
试一试{
响应=连接。建立连接(令牌);
字符串消息=“对不起,连接失败”;
if(响应等于(消息)){
onPostExecute(消息);
}
} 
捕获(例外e)
{
e、 printStackTrace();
}
返回响应;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//在此方法中,您可以打开加载消息
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
试一试{
//Json对象
JSONArray数组=新的JSONArray(结果);
对于(int i=0;i
您可以使用以下方法获取
JsonArray
作为响应

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONArray> listener, ErrorListener errorListener) 
{
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
        listener, errorListener);
}
公共JsonArrayRequest(int方法、字符串url、JSONObject jsonRequest、, 监听器监听器,错误监听器(错误监听器) { super(方法,url,(jsonRequest==null)?null:jsonRequest.toString(), 监听器,错误监听器); }
请参阅以了解更多详细信息。

您不想像这样使用json{“X”:[{…},{…},{…},{…}]}。然后您可以通过这种方式从jsonObject获取jsonArray json.getJsonArray(“X”)您是对的,如果我们想从他体内的jsonObject获取jsonArray,我们必须使用getJsonArray(“Name”)方法,但我认为问题在于侦听器。我想发送一个JSONObjor,我认为最好的选择是做一个JSONObjeTrestQuesty,当我从服务器接收到JSORNARM时,问题就出现了。我想到的第一件事是将侦听器类型更改为JsonArray,但我认为我不能这样做。我认为我应该使用一个定制的请求,我可以在其中执行这样的请求,但我不确定如何执行。在doInBackground()中,您可以发送Json和onPostExecute(),您可以接收Json ArrayTanks,它可能对应用程序的另一部分有用。谢谢,您帮了我很多。