Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put ArrayList into param JsonObject

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:

 private String mUrl;
    private ArrayList<String> mUrlDove;

  HashMap<String, String> params = new HashMap<String, String>();
            params.put("url", mUrl);
            params.put("urlDove", mUrlDove); ---> Wrong because mUrlDove is not a String


            mUrl = app.getInstance().getmUrlRestWS() + getString(R.string.path);

            JsonObjectRequest mRequest = new JsonObjectRequest(
                    mUrl, new JSONObject(params),
                    createMyReqSuccessListener(),
                    createMyReqErrorListener()) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return app.getInstance().createBasicAuthHeader();
                }
            };

If i try with Browser i must set this:

{
  "url": "www.secret.com",
  "urlDove" : [ "www.google.com","www.yahoo.com"]
}
like image 276
DevOps85 Avatar asked Apr 21 '15 17:04

DevOps85


People also ask

How can we convert a list to the JSON array in Java?

We can convert a list to the JSON array using the JSONArray. toJSONString() method and it is a static method of JSONArray, it will convert a list to JSON text and the result is a JSON array.

Can we convert JSONArray to JSONObject?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

How do I create multiple JSON objects?

JSONArray pdoInformation = new JSONArray(); JSONObject pDetail1 = new JSONObject(); JSONObject pDetail2 = new JSONObject(); JSONObject pDetail3 = new JSONObject(); pDetail1. put("productid", 1); pDetail1. put("qty", 3); pDetail1. put("listprice", 9500); pDetail2.

How do I use JSONArray?

jsonObject. put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.


2 Answers

try passing JSONObject instead of hashmap

JSONObject params = new JSONObject();
params.put("url", "www.secret.com");

JSONArray urlDove = new JSONArray();
urlDove.put("www.google.com");
urlDove.put("www.yahoo.com");

params.put("urlDove", urlDove);


JsonObjectRequest mRequest = new JsonObjectRequest(
                mUrl, params,
                createMyReqSuccessListener(),
                createMyReqErrorListener()) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return app.getInstance().createBasicAuthHeader();
            }
        };

for reference in parsing json https://stackoverflow.com/a/17810270/4810752

like image 115
Genevieve Avatar answered Oct 15 '22 21:10

Genevieve


you need to make a JSON Array first and then store that

private String mUrl;
private ArrayList<String> mUrlDove;

HashMap<String, String> params = new HashMap<String, String>();
        params.put("url", mUrl);
        JSONArray jsArray = new JSONArray(mUrlDove);
        params.put("urlDove", jsArray.toString()); 

        mUrl = app.getInstance().getmUrlRestWS() + getString(R.string.path);

        JsonObjectRequest mRequest = new JsonObjectRequest(
                mUrl, new JSONObject(params),
                createMyReqSuccessListener(),
                createMyReqErrorListener()) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return app.getInstance().createBasicAuthHeader();
            }
        };
like image 38
Tomer Shemesh Avatar answered Oct 15 '22 22:10

Tomer Shemesh