简体   繁体   中英

How to call web service using post method with request paramters

Rest web service integrating in request parameter format is like this

    {
  "user" : {
    "id" : null,
    "createdBy" : 1,
    "createdOn" : null,
    "emailAddress" : "goza1@apaservices.net",
    "enabled" : "True",
    "firstName" : "Marietnmnnta ",
    "lastName" : "Zarafftgoza",
    "mobileNo" : "556641488346",
    "status" :  null,
    "updatedBy" : null,
    "updatedOn" : null,
    "regType" : "User",
    "profilePicUrl" : "c:",
    "profile" : null
  }
}

How to call web service using post method with this request ,I have checked so many examples on net but the request parameter format i didn't get ,Some one pls help

just simply create jsonrequest like this

JSONObject jsonBody = new JSONObject();
jsonBody.put("id", null);
jsonBody.put("createdBy", 1);
jsonBody.put("profile", null);

now add your jsonbody to main object

JSONObject jsonMain = new JSONObject();
jsonBody.put("user", jsonBody);

Create the JSONObject for adding your input parameters as below, and attach the same to HttpPost object.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

String json = "";

JSONObject user = new JSONObject();
user.put("id", null); 
user.put("createdBy", 1); 
user.put("createdOn", null);

json = user.toString();

StringEntity se = new StringEntity(json);

httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

HttpResponse httpResponse = httpclient.execute(httpPost);

Try like that. i hope it will help you..!

JSONObject jsonobj = new JSONObject();  
JSONObject geoJsonObj = new JSONObject();  

try { 

    jsonobj.put("action","put-point");  
    geoJsonObj.put("lng", longitude);  
    geoJsonObj.put("lat", latitude);  
    geoJsonObj.put("rangeKey", rangeKey);  
    geoJsonObj.put("schoolName", "TESTSCHOOL535353");  
    jsonobj.put("request", geoJsonObj);  

} catch (JSONException e) {  
    e.printStackTrace();  
}
new SendData().execute(jsonobj.toString());

public class SendData extends AsyncTask<String, Integer, Double>{  

    String response="";  
    @Override  
    protected Double doInBackground(String... params) {  

        postData(params[0]);  

    }  

    public void postData(String jsondata)  {  

        // Create a new HttpClient and Post Header  
        HttpClient httpclient = new DefaultHttpClient();  

        HttpPost httpPost=new HttpPost("url");  
        try {  
            // Add your data  
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
            nameValuePairs.add(new BasicNameValuePair("json",jsondata));  

            httpPost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));  

            // Execute HTTP Post Request  
            HttpResponse res = httpclient.execute(httpPost);  
            InputStream content = res.getEntity().getContent();  

            BufferedReader buffer = new BufferedReader(new InputStreamReader(content));  
            String s = "";  
            while ((s = buffer.readLine()) != null) {  
                response += s;  
            }  
            System.out.println("response from server"+response);  

        } catch (ClientProtocolException e) {  
             // TODO Auto-generated catch block  
        } catch (IOException e) {  
             // TODO Auto-generated catch block  
        }  
    }  
}  

SERVER SIDE-

protected void doPost(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {  

    String jsondata=request.getParameter("json");  

    //now parse your data from json

    try {
        JSONObject JsonObject=new JSONObject(jsondata);  
        JSONObject object=JsonObject.getJSONObject("request");  
        String action=object.getString("action");  
        String lng=object.getString("lng");  
        String lat=object.getString("lat");  
        String rangeKey=object.getString("rangeKey");  
        String schoolName=object.getString("schoolName");  

    } catch (JSONException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  
} 

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