将ArrayList放入参数JsonObject

我必须通过Volley Framework提出要求。这是带有JSONObject的POST请求。

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

我从这个开始:

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

}

};

如果我尝试使用浏览器,则必须进行以下设置:

{

"url": "www.secret.com",

"urlDove" : [ "www.google.com","www.yahoo.com"]

}

回答:

您需要先制作一个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();

}

};

以上是 将ArrayList放入参数JsonObject 的全部内容, 来源链接: utcz.com/qa/424931.html

回到顶部