如何在 Volley, Android 中使用 getParams 返回 JsonObject?



我尝试将此json发送到服务器:

{"name":"apiName","param":{}}

我为它使用 Volley 库并覆盖 getParams 方法,如下所示:

protected Map<String, String> getParams() throws AuthFailureError
{
}

在此方法中,我创建了两个 json 对象:

JSONObject jsonObject = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
try
{
jsonObject.put("name", "apiName");
jsonObject.put("param", jsonObject2);
}
catch (JSONException e)
{
e.printStackTrace();
}

如何在有或没有getParams的情况下发送它?

解决方案:

final String jsonString = jsonObject.toString();
@Override
public byte[] getBody() throws AuthFailureError
{
try { return jsonString == null ? null : jsonString.getBytes("utf-8"); }
catch (UnsupportedEncodingException ex) { return null; }
}

最新更新