【问题标题】:How to send nested json objects inside a request body如何在请求正文中发送嵌套的 json 对象
【发布时间】:2017-08-05 20:58:20
【问题描述】:

我如何在凌空请求的正文中发送此请求, 在此先感谢

{
  "amount": "000",
  "card": {
    "number": "00000",
    "expiry_month": "00",
    "expiry_year": "00",
    "cvv": "000",
    "pin": "000"
  }
}

这是我的请求参数,我试过这个api告诉我无效的参数,请各位高手帮帮我

 @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", User_Token);
            headers.put("Content-Type", "application/json");
            return headers;
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }



        @Override
        public byte[] getBody() {
            Map<String, String> params = new HashMap<String, String>();
            params.put( "amount", amount);
            params.put( "card", String.valueOf(come()));

            return new JSONObject(params).toString().getBytes();
        }
        private byte[] come() {
            Map<String, String> params = new HashMap<String, String>();
            params.put( "number", number);
            params.put( "expiry_month", month);
            params.put( "expiry_year", year);
            params.put( "cvv", cvv);
            params.put( "pin", pin);
            return new JSONObject(params).toString().getBytes();
        }

【问题讨论】:

  • 指定哪个 API 给你一个错误?这是服务器响应还是什么?还要指定错误本身
  • 服务器响应,错误是无效参数
  • 响应只是意味着 api 无法理解我的请求,我希望我的代码有更好的格式
  • 那跟安卓没关系。您能否提供指向服务器 API 文档的链接,并指定完整的请求,包括使用的 HTTP 方法和端点。
  • 对不起,我无法提供 api 的链接,我想要的是如何向服务器发送格式为格式的请求,请帮助我

标签: java android android-volley


【解决方案1】:

使用JsonObject 代替:创建Map,将其转换为Byte[],然后获取其String.valueOf(...)。 这将允许您将完整的对象作为请求的主体发送,而不是现在发送的不完整的主体:{"amount":"000","card":"[B@a2e...."})

"card":"[B@a2e...." 发送的值的问题在于它不是您的对象及其属性的表示。相反,它只是您创建的字节数组的内存地址。

在您的代码中,为您的对象使用JsonObject,并且只在getBody() 方法的末尾转换为Byte[],因为这是您最终返回完整对象的地方:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", User_Token);
    // headers.put("Content-Type", "application/json");  // This is probably redundant, as you are already setting it on getBodyContentType()
    return headers;
}

@Override
public String getBodyContentType() {
    return "application/json";
}

@Override
public byte[] getBody() {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("amount", amount);
        jsonObject.put("card", come());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObject.toString().getBytes();
}

private JSONObject come() throws JSONException {
    JSONObject params = new JSONObject();
    params.put( "number", number);
    params.put( "expiry_month", month);
    params.put( "expiry_year", year);
    params.put( "cvv", cvv);
    params.put( "pin", pin);
    return params;
}

【讨论】:

  • 您是否尝试过使用 postman、curl 或其他工具向您的服务发出请求?
  • 我正在使用 postmn,它在 postman 上完美运行,其他 guz 编写网络已完成。这是链接 https//:api.myflex。 ng/钱包/基金
  • @kehindehussein 根据docs,没有指定wallet/fund。你从哪里得到这个端点的规范?
【解决方案2】:

如果您需要精确的格式,您需要确保 numbermonth 等的值作为字符串而不是整数传递。 此外,为了使代码看起来更短且破坏性更小,您可以链接 put 调用。这是一个稍微重构的版本:

@Override
public byte[] getBody() {  
    try {
        final JSONObject card = new JSONObject()
                .put("number", String.valueOf(number))
                .put("expiry_month", String.valueOf(month))
                .put("expiry_year", String.valueOf(year))
                .put("cvv", String.valueOf(cvv))
                .put("pin", String.valueOf(pin));

        return new JSONObject()
                .put("amount", String.valueOf(amount))
                .put("card", card)
                .toString()
                .getBytes();    
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Empty json return will not help us as in this case 
    //we will send useless empty body to the server. 
    return null;
}

我不知道getBody() 的调用方发生了什么以及您是否能够修改该代码。但是您需要以某种方式跟踪 null 返回,或者引入您自己的异常类,该类应该在 getBody() 调用方进行检查。但是如果你无法控制getBody()调用方,你需要返回一些非null,空返回:

    return new byte[0];

【讨论】:

  • 它给出了同样的错误“无效参数”请帮助我
  • 我提供的代码应该生成与您请求的结构相同的 JSON 对象。如果服务器响应“无效参数”,我们如何在不知道服务器期望什么和不期望什么的情况下帮助您?
  • 这是api链接https//:api.myflex.ng/wallet/fund
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 2018-05-07
  • 2017-06-23
相关资源
最近更新 更多