Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopj Post request with body

Dear all i am using Loopj and really like it. It makes my life easier. Now I want post json in the body of the post request.kindly check it what i m doing wrong my code is below.

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");

Then i call like this.

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

I got this error

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

Kindly tell me how to send json array of objects in the body of the post request using LoopJ. best regards,

like image 580
DroidEngineer Avatar asked Apr 18 '26 22:04

DroidEngineer


1 Answers

I think this is what you're looking for:

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();
}
like image 197
Uri Avatar answered Apr 26 '26 04:04

Uri