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

时间:2013-11-25 07:32:46

标签: java android json

您好我想将一个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
    }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

发送字符串实体

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

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