我正在使用Volley Android将一些数据发布到Restful Api端点,使用邮递员它可以在raw
和application/json
中运行很好的正文数据,例如:
{
"operation": "someaction",
"key": "pk_test_fssdfsdfjkhkjsdhf84334",
"token": "tk_test_432332sdxxaJJJJHHHshhsas",
"ssids":[[{
"mac":"34:15:13:d4:59:f1" //<--this is important here, it's mac addr
}]] //<-- yup, with double double [[ ... ]]
}
当我在下面的代码中使用截击时,问题就开始了:
public void getBleeCardData(Response.Listener<String> response, final String ssidsdata, final ProgressDialog pd)throws JSONException {
String url = "https://www......com/api/something.do";
JSONObject jsonBody = new JSONObject();
jsonBody.put("operation", "something");
jsonBody.put("key", "pk_try_oqiwFHFKjkj4XMrh");
jsonBody.put("token", "tk_tkn_erwweelWgH4olfk2");
jsonBody.put("ssids", ssidsdata.toLowerCase());
final String mRequestBody = jsonBody.toString();
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR", "error => " + error.toString());
}
}
) {
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
};
queue.add(postRequest.setRetryPolicy(new DefaultRetryPolicy(30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT))); }
我需要编码方面的帮助,使用Volley发送的数据如下:
{ “操作”: “dataMachines”, “关键”: “pk_test_6pRNAHGGoqiwFHFKjkj4XMrh”, “标记”: “tk_test_ZQokik736473jklWgH4olfk2”, “的SSID”: “[[{\” MAC \ “:\” 34:15:13: D4:59:F1 \ “}]]”}
为什么像"ssids":"[[{\"mac\":\"34:15:13:d4:59:f1\"}]]"}
这样发送此节点?如何"\"
,删除它的某些特殊编码?
是否可以更改编码以阻止"\"
?
答案 0 :(得分:0)
创建一个JsonObjectRequest
JSONObject jsonBody = new JSONObject();
jsonBody.put("operation", "something");
jsonBody.put("key", "pk_try_oqiwFHFKjkj4XMrh");
jsonBody.put("token", "tk_tkn_erwweelWgH4olfk2");
jsonBody.put("ssids", ssidsdata.toLowerCase());
JsonObjectRequest jobReq = new JsonObjectRequest(Request.Method.POST, url, jsonBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
//Log.d("Responses", jsonObject.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Log.e("responses", volleyError.toString());
}
});
如果您要发送JSONArray,则使用JsonArrayRequest而不是JsonObjectRequest
答案 1 :(得分:0)
根据android代码,在解析对象时,下面需要一个字符串:
jsonBody.put("ssids", ssidsdata.toLowerCase());
你可以单独制作ssid然后将它添加到json体中,如下所示:
JSONObject ssid= new JSONObject();
ssid.put("mac":"34:15:13:d4:59:f1");
ssid.put("key2","Value2");
将ssid对象添加到Json对象中以发送:
JSONObject jsonBody = new JSONObject();
jsonBody.put("operation", "something");
jsonBody.put("key", "pk_try_oqiwFHFKjkj4XMrh");
jsonBody.put("token", "tk_tkn_erwweelWgH4olfk2");
jsonBody.put("ssids", ssid);
在此解释:Pass an object containing key value pairs, as a value to a hashmap in Java/Android
答案 2 :(得分:0)
JSONObject obmacAdd=new JSONObject();
JSONArray array=new JSONArray();
obmacAdd.put("mac","34:15:13:d4:59:f1");
array.put(obmacAdd.toString());
现在
jsonBody.put("ssids", array.toString());