Put ArrayList into param JsonObject

前端 未结 3 1421
再見小時候
再見小時候 2020-12-19 11:30

i must do i request with Volley Framework. This is a POST request with JSONObject.

I must pass one string and one JSONArray..but how i can?

I start with this

3条回答
  •  天涯浪人
    2020-12-19 12:08

    JSONObject can take in Java Objects, try using

    Map 
    

    something like this:

        String mUrl;  //initialized somewhere else
        ArrayList mUrlDove;  //initialized somewhere else
    
        Map jsonParams = new HashMap<>();
        jsonParams.put("url", mUrl);
        jsonParams.put("urlDove", mUrlDove);
    
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
                new Response.Listener()
                {
                    @Override
                    public void onResponse(JSONObject response)
                    {
                        Log.d("Volley Response: ", response.toString()); 
                        //do the other stuff you need...
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        if (null != error.networkResponse)
                        {
                            Log.d(" Volley Error Code: ", "" + error.networkResponse.statusCode);
                           //probably throw an Exception or some MessageEvent
                        }
                    }
                });
    
        requestQueue.add(request);
    

    this works for me with complex objects like

     Map>>
    

    with the most inner objects being Strings and Integers, and the List being initialized as a new ArrayList.

    Hope this Helps!

提交回复
热议问题