Loopj Post请求身体

时间:2012-12-18 11:59:02

标签: post request loopj

亲爱的我正在使用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对象数组。 最好的问候,

1 个答案:

答案 0 :(得分:6)

我认为这就是你要找的东西:

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