繁体   English   中英

如何使用HttpPost将Json发送到Web服务

[英]How to send a Json to a web service using HttpPost

嗨,我想将json对象发送到Web服务,我几乎尝试了所有尝试,但均未成功。 当Web服务接收到数据时,它返回“ eureka”,因此我也希望能够看到响应。

public void sendData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://pruebaproyectosmi.azurewebsites.net/home/Insert?data=");

    try {
        httppost.setEntity(new UrlEncodedFormEntity(json));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}
private void SendBookingData(final String SendCustomerId,final String SendCustomerName, final String BookingDate,
            final String BookingTime, final String SendNetAmount,final String SendTotalAmount, final String SendTotalQuantity,
            final String SendDeliveryDate, final String GetBranchId,final String Senduserid,final String Sendratelistid) {

                    HttpClient client = new DefaultHttpClient();
                    JSONObject json = new JSONObject();
                    try {


                        String SendBookingURL= "your url";
                        HttpPost post = new HttpPost(SendBookingURL);       
                        HttpResponse response;
                        json.put("GetcustomerName", SendCustomerName);
                        json.put("GetBookingDate",BookingDate);
                        json.put("GetTotalCost", SendTotalAmount);
                        json.put("GetNetAmount", SendNetAmount);
                        json.put("GetTotalQuantity",SendTotalQuantity );
                        json.put("GetCustomerId", SendCustomerId);
                        json.put("GetDeliveryDate", SendDeliveryDate);
                        json.put("GetBookingtime", BookingTime);
                        json.put("GetBranchId", GetBranchId);
                        json.put("GetUserId", Senduserid);
                        json.put("GetRateListId", Sendratelistid);
                        StringEntity se = new StringEntity( json.toString()); 
                        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                        post.setEntity(se);
                        try {
                            response = client.execute(post);
                            HttpEntity entity = response.getEntity();
                            if(entity != null) {
                                ResponseSummaryTable = EntityUtils.toString(entity);
                                System.out.println("body" + ResponseSummaryTable);
                            }
                        }
                          catch (Exception e) {
                                e.printStackTrace();
                            }
                       }
                            catch(Exception e){
                                e.printStackTrace();
                            }       
                       }

发送字符串实体

码:

HttpClient client = new DefaultHttpClient();
HttpUriRequest request;
request = new HttpPost(<-URL->);
StringEntity entity = new StringEntity(<-Your JSON string->);

((HttpPost) request).setEntity(entity);
((HttpPost) request).setHeader("Content-Type",
                    "application/json");

 HttpResponse response = client.execute(request);
 HttpEntity entity = response.getEntity();

此代码将json作为字符串实体发送到服务器,并接收HttpEntity作为响应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM