简体   繁体   中英

How to send a Json to a web service using HttpPost

Hi I want to send a json object to a web service , I have tried almost everything without success. When the webservice recives the data it returns "eureka" , so I want to be able to see the response too.

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

Send string entity instead

CODE:

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

This code will send json as string entity to server and receives HttpEntity as response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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