VolleyPost请求,数组形式的参数有的手机提交不了

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lmy0111ly/article/details/50418476
VolleyPost请求,数组形式的参数有的手机提交不了,试了好多次,才发现可能数组传的有问题,先贴错误代码:

        HashMap<String, Object> params = new HashMap<>();
        params.put("name", mAddBrandName);
        if (categoryIds != null) {
            params.put("categoryIds", categoryIds);
        }
        for (int i = 0; i < categoryIds.length; i++) {
            System.out.println(categoryIds[i]);
        }
        JSONObject json = new JSONObject(params);
        VolleyRequest.jsonRequest(this, Request.Method.POST, Constant.Brand_Save, Constant.HGJ_TAG, json, new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(JSONObject result) {
             
                if (result != null) {
                    try {
                        if (result.getInt("code") == 200) {
                            CommonUtils.showToastMsg(AddNewBrandActivity.this, "添加成功");
                            finish();
                        } else {
                            CommonUtils.showToastMsg(AddNewBrandActivity.this, result.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onMyError(VolleyError error) {
                CommonUtils.showToastMsg(AddNewBrandActivity.this, "请求失败");
            }
        });
 
 
此次提交的数据参数中,categoryIds是一个数组,我在魅族手机,小米手机,三星手机中可以提交成功,但谷歌模拟器,OPPO等手机就请求失败。这次换了JSON解析的方法,这次成功了。代码如下:
 
 
 
 

        HashMap<String, Object> params = new HashMap<>();
        params.put("name", mAddBrandName);
        if (categoryIds != null) {
            params.put("categoryIds", categoryIds);
        }
        for (int i = 0; i < categoryIds.length; i++) {
            System.out.println(categoryIds[i]);
        }
//        {"name":"好利来","categoryIds":[2,3]}
        String strJson = GsonUtils.parseMapToJson(params);
        JSONObject json = null;
        try {
            json = new JSONObject(strJson);
        } catch (JSONException e) {
            e.printStackTrace();
        }
 
剩余代码都一样哦.
 

猜你喜欢

转载自blog.csdn.net/lmy0111ly/article/details/50418476