java 将 ArrayList 放入参数 JsonObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29779447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:55:47  来源:igfitidea点击:

Put ArrayList into param JsonObject

javaandroidandroid-volley

提问by DevOps85

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

我必须使用 Volley Framework 请求。这是一个带有 JSONObject 的 POST 请求。

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

我必须传递一个字符串和一个 JSONArray..但是我怎么能?

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"]
}

采纳答案by Tomer Shemesh

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

你需要先制作一个 JSON 数组,然后存储它

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

回答by Genevieve

try passing JSONObject instead of hashmap

尝试传递 JSONObject 而不是 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

解析json参考https://stackoverflow.com/a/17810270/4810752

回答by TommySM

JSONObject can take in Java Objects, try using

JSONObject 可以接收 Java Objects,尝试使用

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.

最内部的对象是字符串和整数,并且列表被初始化为一个新的 ArrayList。

Hope this Helps!

希望这可以帮助!