2012-12-18 75 views
2

亲爱的我所有使用Loopj,真的很喜欢它。它让我的生活更轻松。现在我想要post json在发布请求的主体中。请检查它在做什么错我的代码在下面。Loopj发布请求正文

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} 

请告诉我如何发送对象的JSON数组使用循环J POST请求的身体。 最好的问候,

+0

好像与URL一些问题,您正在使用(WEBCONSTANTS.ORDER_SERVICE_SAVE_ORDER)。请使用浏览器(扩展名,例如高级REST客户端)检查POST第一个 – Sangharsh

回答

4

我认为这是你在找什么:

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

是的,但它不工作,我认为loopj后不会发布完整的字符串在server.might是它修剪它。 – DroidEngineer

+0

上面的代码适用于我,你得到了什么错误信息? – Uri

+1

**服务器没有收到我在邮件正文中发送的http字符串。 我这样做, AsyncHttpClient mAsyncHttpClient = new AsyncHttpClient(); mAsyncHttpClient.addHeader(“Content-Type:”,“application/json”); (“Accept”,“application/json”); Gson mGson = new Gson(); final String mGSON = mGson.toJson(ObjectToSendToServer); mAsyncHttpClient \t \t \t \t \t .POST(mContext, \t \t \t \t \t \t \t “HTTP:// ServerIPAddress/SaveSaleOrderWithJson”, \t \t \t \t \t \t \t(新StringEntity(mGSON)),“应用/ JSON”, \t \t \t \t \t \t \t new AsyncHttpResponseHandler(){} ** – DroidEngineer