【问题标题】:Send a json request in Volley在 Volley 中发送 json 请求
【发布时间】:2018-02-16 07:34:22
【问题描述】:

Android 上使用Volley 发送特定的json 请求时遇到问题。

我正在使用以下内容发送简单的 json 请求:

final JSONObject jsonObject = new JSONObject();
try {
    jsonObject.put("userId", "testUserId");
    jsonObject.put("password", "testPass");
} catch (JSONException e) {
    Log.d("JSONexception","JSON EXCEPITON: "+e.tostring());
}

但是对于更复杂的请求,我不确定如何创建 json 正文请求。这是我要创建的 json 请求的示例:

{
“userId” : “testUserId”,
“password” : “testPass”,
“validParameters” : [
    {
        “key” : “testKey”,
        “value” : “testValue”
    }
],
“requestTimestamp” : null
}

【问题讨论】:

  • 你需要学习一些 JSON 基础知识......你需要添加名为“validParameters”的 JSONArray,然后使用“key”和“value”属性将新的 JSONObject 添加到这个数组中......

标签: android json request android-volley


【解决方案1】:
final JSONObject jsonObject = new JSONObject();
final JSONArray  jsonArray =new JSONArray();
final JSONObject newjsonObject =new JSONObject();  
try {
jsonObject.put("userId", "testUserId");
jsonObject.put("password", "testPass");
jsonObject.put("validParameters",jsonArray );
jsonArray.put(newjsonObject); 
newjsonObject.put("key","testKey"); 
newjsonObject.put("value","testValue"); 

} catch (JSONException e) {
Log.d("JSONexception","JSON EXCEPITON: "+e.tostring());
}

试试这个

【讨论】:

  • 感谢支持,对于我给出的 json 示例,它按预期工作,现在我可以将它与任何其他 json 配置一起使用。
  • 其实 json 并没有特定的结构,而且在不同的情况下会有所不同。上述案例是 jsonObject 中的 jsonArray 的示例,类似地,jsonArray 中也可以有 jsonObject 案例。因此,您需要根据结构的要求设计柱体。如果您尝试一些示例来理解这一点会更好
猜你喜欢
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
相关资源
最近更新 更多