Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RequestMapping(value = "/addUser", method = RequestMethod.POST)
- public String addUser(@RequestParam(value="firstname") String firstname,
- @RequestParam(value="lastname") String lastname,
- @RequestParam(value="username") String username,
- @RequestParam(value="password") String password) throws Exception {
- String success = add.addUser(firstname, lastname, username, password);
- return toJson(success);
- }
- var data = {
- firstname: firstname,
- lastname: lastname,
- username : username,
- password : password
- };
- $.ajax({
- type: 'POST',
- url: "<url removed>/import/addUser",
- success: function(data) {
- callbackSuccess(JSON.stringify(data));
- },
- error: function(data) {
- callbackFail(data);
- },
- data: data,
- dataType: "json"
- });
- String url = "<url removed>/import/addUser";
- InputStream inputStream = null;
- String result = "";
- try {
- // 1. create HttpClient
- HttpClient httpclient = new DefaultHttpClient();
- // 2. make POST request to the given URL
- HttpPost httpPost = new HttpPost(url);
- String json = "";
- // 3. build jsonObject
- JSONObject jsonObject = new JSONObject();
- jsonObject.accumulate("firstname", userFirstName);
- jsonObject.accumulate("lastname", userLastName);
- jsonObject.accumulate("username", emailStr);
- jsonObject.accumulate("password", passwordStr);
- // 4. convert JSONObject to JSON to String
- json = jsonObject.toString();
- // 5. set json to StringEntity
- StringEntity se = new StringEntity(json);
- // 6. set httpPost Entity
- httpPost.setEntity(se);
- Log.i("Sending", json);
- // 7. Set some headers to inform server about the type of the content
- httpPost.setHeader("Accept", "application/json");
- httpPost.setHeader("Content-type", "application/json");
- // 8. Execute POST request to the given URL
- HttpResponse httpResponse = httpclient.execute(httpPost);
- // 9. receive response as inputStream
- inputStream = httpResponse.getEntity().getContent();
- // 10. convert inputstream to string
- if(inputStream != null)
- result = convertInputStreamToString(inputStream);
- else
- result = "Did not work!";
- Log.i("Response", result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement