Put ArrayList into param JsonObject
Asked Answered
A

3

6

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"]
}
Additive answered 21/4, 2015 at 17:40 Comment(0)
F
6

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();
            }
        };
Fixation answered 21/4, 2015 at 18:8 Comment(2)
com.android.volley.ParseError: org.json.JSONException: Value Created of type java.lang.String cannot be converted to JSONObjectSelfacting
i need help here #46423227Auster
K
5

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://mcmap.net/q/166962/-android-create-json-array-and-json-object

Kierakieran answered 21/4, 2015 at 18:10 Comment(0)
S
2

JSONObject can take in Java Objects, try using

Map<String,Object> 

something like this:

    String mUrl;  //initialized somewhere else
    ArrayList<String> mUrlDove;  //initialized somewhere else

    Map<String, Object> 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<JSONObject>()
            {
                @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<String,<List<Map<String,Object>>>

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

Hope this Helps!

Scone answered 22/4, 2015 at 8:36 Comment(2)
can you help #46423227Auster
@Auster still relevant?Scone

© 2022 - 2024 — McMap. All rights reserved.