Android Volley POST - 正文中的 JSON 编码

marcode_ely

我正在使用 Volley Android 将一些数据发布到 Restful Api 端点,使用邮递员可以很好地处理身体数据rawapplication/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 [[ ... ]]
}

它使用 POSTMAN 工作并返回 在此处输入图片说明

当我在下面的代码中使用 volley 时,问题就开始了:

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","key":"pk_test_6pRNAHGGoqiwFHFKjkj4XMrh","token":"tk_test_ZQokik736473jklWgH4olfk2","ssids":"[[{\"mac\"5:\"59d1: :f1\"}]]"}

为什么像"ssids":"[[{\"mac\":\"34:15:13:d4:59:f1\"}]]"}这样发送这个节点?,怎么样"\",一些特殊的编码来删除它?

是否可以更改编码以防止这些"\"

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Volley Post请求在正文中带有json对象,并在String中获取响应

正文中的Android Volley POST字符串

如何在Android中使用Volley在POST请求的正文中传递数组

使用Retrofit Android在POST请求中从JSON正文中排除某些字段

如何使用Volley发送带有JSON正文的POST请求?

带有标题和 JSON 正文的 Volley 中的 POST 请求

如何正确使用POST的JSON响应正文中的数据?

您可以在Guzzle POST正文中包含原始JSON吗?

Android Volley:错误的网址编码

POST正文中的JSON,然后提取响应的正文以获取access_token

Android Volley JSON响应

JSON对象的Volley Post方法

如何获得Volley POST响应正文?

客户端发送的请求在语法上不正确-JSON POST正文中的类型复杂

如何访问POST正文中发送的已转换JSON对象的字段?

如果URL中传递了多个参数,则Django无法在POST请求正文中加载Json

无法将POST请求正文中的json绑定到.net-core中的类参数

JSON POST正文中字符串ID或对象属性的C#Web API处理

实体框架 ASP.NET;HTTP POST:JSON 请求正文中存在所需的值 DbEntityValidationException

为什么 POST 请求正文中的字符串带有额外的引号而不是 JSON 对象

JSON正文中的变量访问

GuzzleHttp - 获取 JSON 编码的正文

JSON 格式的 Android Volley 数组

Dart POST 表单 url 编码的正文

Android Volley - 无法使用 POST 执行 JSON 请求,得到空响应

無法使用 Volley StringRequest 在正文中發布 JSONObject

POST期间,请求正文中的变量被{}替换

POST请求正文中的空绑定

从 post 请求正文中获取值