Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. @RequestMapping(value = "/addUser", method = RequestMethod.POST)
  2. public String addUser(@RequestParam(value="firstname") String firstname,
  3. @RequestParam(value="lastname") String lastname,
  4. @RequestParam(value="username") String username,
  5. @RequestParam(value="password") String password) throws Exception {
  6. String success = add.addUser(firstname, lastname, username, password);
  7. return toJson(success);
  8. }
  9.  
  10. var data = {
  11. firstname: firstname,
  12. lastname: lastname,
  13. username : username,
  14. password : password
  15. };
  16. $.ajax({
  17. type: 'POST',
  18. url: "<url removed>/import/addUser",
  19. success: function(data) {
  20. callbackSuccess(JSON.stringify(data));
  21. },
  22. error: function(data) {
  23. callbackFail(data);
  24. },
  25. data: data,
  26. dataType: "json"
  27. });
  28.  
  29. String url = "<url removed>/import/addUser";
  30. InputStream inputStream = null;
  31. String result = "";
  32. try {
  33. // 1. create HttpClient
  34. HttpClient httpclient = new DefaultHttpClient();
  35.  
  36. // 2. make POST request to the given URL
  37. HttpPost httpPost = new HttpPost(url);
  38. String json = "";
  39. // 3. build jsonObject
  40. JSONObject jsonObject = new JSONObject();
  41. jsonObject.accumulate("firstname", userFirstName);
  42. jsonObject.accumulate("lastname", userLastName);
  43. jsonObject.accumulate("username", emailStr);
  44. jsonObject.accumulate("password", passwordStr);
  45.  
  46. // 4. convert JSONObject to JSON to String
  47. json = jsonObject.toString();
  48.  
  49. // 5. set json to StringEntity
  50. StringEntity se = new StringEntity(json);
  51.  
  52. // 6. set httpPost Entity
  53. httpPost.setEntity(se);
  54. Log.i("Sending", json);
  55. // 7. Set some headers to inform server about the type of the content
  56. httpPost.setHeader("Accept", "application/json");
  57. httpPost.setHeader("Content-type", "application/json");
  58.  
  59. // 8. Execute POST request to the given URL
  60. HttpResponse httpResponse = httpclient.execute(httpPost);
  61.  
  62. // 9. receive response as inputStream
  63. inputStream = httpResponse.getEntity().getContent();
  64.  
  65. // 10. convert inputstream to string
  66. if(inputStream != null)
  67. result = convertInputStreamToString(inputStream);
  68. else
  69. result = "Did not work!";
  70.  
  71. Log.i("Response", result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement