在Java POST请求中将JSON对象作为参数传递

时间:2018-11-27 22:56:40

标签: java android http-post

这可能是一个愚蠢的问题,但我正在尝试将JSON对象作为参数包含在来自Android应用的发布请求中。当后端接受请求时,它认为该对象为空,并且我相信是因为密钥被视为字符串。这是我记录对象的代码:

let address = req.body.shippingAddress;
console.log(address);

这是日志输出:

{"zipCode":"00000","country":"US","city":"New York","name":"Ross","state":"NY","line2":"","email":"ross@example.com","line1":"123 fake st"}

与此同时,当我从iOS应用发布时,日志相同:

{ email: 'ross@example.com',
  city: 'New York',
  line1: '123 fake st',
  name: 'Ross',
  zipCode: '00000',
  line2: '',
  state: 'NY',
  country: 'US' 
}

这是我的帖子代码:

Map<String, Object> paramMap = new HashMap<>();
Gson gson = new Gson();
String shippingJson = gson.toJson(addressMap);
paramMap.put("shippingAddress", shippingJson);
mCompositeSubscription.add(
            mStripeService.createPaymentIntent(paramMap)
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Action1<ResponseBody>() {
                        @Override
                        public void call(ResponseBody response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response.string());
                                listener.onObjectResponse(jsonObject);
                            } catch (JSONException exception) {
                                Log.e("Stripe", exception.toString());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            listener.onObjectResponse(throwable);
                        }
                    })
);

条纹服务:

@FormUrlEncoded
@POST("stripeCreateCharge")
Observable<ResponseBody> createPaymentIntent(@FieldMap Map<String, Object> params);

0 个答案:

没有答案