如何在凌空机器人上发送对象并获得数组响应?

时间:2018-12-12 10:18:40

标签: android

我需要发送对象值

{
    "Fromdate":"04/11/2018",
    "Todate":"11/11/2018",
    "Task":"abc"
}

我在数组中得到响应

[{}]

请帮助我

感谢前进。

1 个答案:

答案 0 :(得分:0)

如果您可以通过对问题的更多解释来编辑问题,那对我帮助很大。但是我可能知道您的需求。

Volley等待JsonRequest排队,但是实际上您可以按照自己想要的方式管理它,如果服务器的响应是JsonArray并且要发送JsonObject,则可能需要以下方法。

>

您必须扩展JsonRequest并处理响应:

private class CustomJsonArrayRequest extends JsonRequest<JSONArray> {
    public CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}

然后您可以设置您的请求:

JSONObject json = new JSONObject();
try {
    json.put("Fromdate", "04/11/2018");
    json.put("Todate", "11/11/2018");
    json.put("Task", "abc");
} catch( JSONException e){
    Log.e("ErrorBuildingJson", "Error building request JSONObject");
    e.printStackTrace();
    json = null; //GC this
}

//checkConnection is a custom method
if (json != null && checkConnection()) {
    // Instantiate the RequestQueue
    RequestQueue queue = Volley.newRequestQueue(this);

    // Request a string response from the provided URL.
    CustomJsonArrayRequest jsonRequest = new CustomJsonArrayRequest(Request.Method.POST, myURI, json,
            new Response.Listener<JSONArray>(){
                @Override
                public void onResponse(JSONArray response) {
                    if (response != null && response.length() > 0) { //If the response is valid
                        // Do Stuff
                    } else { //If the response is not valid, the request also failed
                        Log.e("ErrorOnRequest", "The server responded correctly, but with an empty array!");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("ErrorOnResponse", "Response from the server contains errors!");
                }
            }
    );
    // Add the request to the RequestQueue.
    queue.add(jsonRequest);
}

请注意,这种方法希望您的服务器等待一个简单的json对象并返回一个json数组作为响应。

这是一种非常常见的方法,可以在许多存储库中找到,因为它使您可以控制请求本身。

示例:https://github.com/stefankorun/najdisme/blob/master/src/mk/korun/najdismestuvanje/net/CustomJsonArrayRequest.java

希望对您有帮助。