3

亲爱的,我正在使用Loopj并且非常喜欢它。它让我的生活更轻松。现在我想在帖子请求的正文中发布 json。请检查我做错了什么我的代码在下面。

params.put("SaleOrderItems", mJsonArrayOfObject.toString());
    params.put("CustomerReferenceNumber", "asdf");
    // /*mSaleOrder.getmCustomerReferenceNo()*/);
    params.put("RecordType", "HOS");
    params.put("DeliveryDate", "2012-12-28T12:04:27.3553985+01:00"); // mSaleOrder.getmDeliveryDate());
    params.put("SellToCustomerNumber", "user");

然后我这样打电话。

mAsyncHttpClient.post(WEBCONSTANTS.ORDER_SERVICE_SAVE_ORDER, mParams,
            new AsyncHttpResponseHandler(){};

我收到了这个错误

{"Message":"No HTTP resource was found that matches the request URI}

请告诉我如何使用 LoopJ 在 post 请求的正文中发送对象的 json 数组。此致,

4

1 回答 1

7

我想这就是你要找的:

String url = "<your site url>";
JSONObject jdata = new JSONObject();
try {
  jdata.put("key1", val1);
  jdata.put("key2", val2);
} catch (Exception ex) {
  // json exception
}
StringEntity entity;
try {
  entity = new StringEntity(jdata.toString());
  client.post(getBaseContext(), url, entity, "application/json", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
      JSONObject res;
      try {
        res = new JSONObject(response);
        Log.d("debug", res.getString("some_key")); // this is how you get a value out
      } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

  });
} catch (UnsupportedEncodingException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
}
于 2012-12-26T00:36:46.413 回答