Guest User

Untitled

a guest
Dec 19th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. [HttpPost]
  2. public string ValidateAccount(string user, string password)
  3. {
  4. UserAccount account = new UserAccount();
  5. return account.Login(user, password);
  6. }
  7.  
  8. public class SendPostRequest extends AsyncTask<String, Void, String>
  9. {
  10. String username = edt_user.getText().toString();
  11. String password = edt_pass.getText().toString();
  12.  
  13. @Override
  14. protected void onPreExecute() {
  15. pb_bar.setVisibility(View.VISIBLE);
  16. }
  17.  
  18. @Override
  19. protected String doInBackground(String... params) {
  20. HttpURLConnection conn = null;
  21. try
  22. {
  23. java.net.URL url = new URL("http://192.190.191.97:65/api/AuthenticateUser/ValidateAccount");
  24. JSONObject postDataParams = new JSONObject();
  25. postDataParams.put("user", username);
  26. postDataParams.put("password", password);
  27.  
  28. Log.e("params", postDataParams.toString());
  29.  
  30. conn = (HttpURLConnection) url.openConnection();
  31. conn.setReadTimeout(15000); /* milliseconds */
  32. conn.setConnectTimeout(15000); /* milliseconds */
  33. conn.setRequestMethod("POST");
  34. conn.setDoInput(true);
  35. conn.setDoOutput(true);
  36.  
  37. OutputStream os = conn.getOutputStream();
  38. BufferedWriter writer = new BufferedWriter(
  39. new OutputStreamWriter(os, "UTF-8"));
  40. writer.write(getPostDataString(postDataParams));
  41.  
  42. writer.flush();
  43. writer.close();
  44. os.close();
  45.  
  46. int responseCode = conn.getResponseCode();
  47.  
  48. if (responseCode == HttpsURLConnection.HTTP_OK)
  49. {
  50. BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  51. StringBuffer sb = new StringBuffer("");
  52.  
  53. String line = "";
  54. while ((line = in.readLine()) != null)
  55. {
  56. sb.append(line);
  57. break;
  58. }
  59. in.close();
  60. return sb.toString();
  61. }
  62. else
  63. {
  64. return new String("false : " + responseCode);
  65. }
  66. }
  67. catch (Exception e) {
  68. return new String("Exception: " + e.getMessage());
  69. }
  70. finally
  71. {
  72. conn.disconnect();
  73. }
  74.  
  75. }
  76.  
  77. @Override
  78. protected void onPostExecute(String r) {
  79. pb_bar.setVisibility(View.GONE);
  80. Toast.makeText(getApplicationContext(), r, Toast.LENGTH_LONG).show();
  81.  
  82. }
  83.  
  84. public String getPostDataString(JSONObject params) throws Exception
  85. {
  86. StringBuilder result = new StringBuilder();
  87. boolean first = true;
  88. Iterator<String> itr = params.keys();
  89. while (itr.hasNext())
  90. {
  91. String key = itr.next();
  92. Object value = params.get(key);
  93.  
  94. if (first)
  95. first = false;
  96. else
  97. result.append("&");
  98.  
  99. result.append(URLEncoder.encode(key, "UTF-8"));
  100. result.append("=");
  101. result.append(URLEncoder.encode(value.toString(), "UTF-8"));
  102.  
  103. }
  104. return result.toString();
  105. }
  106. }
Add Comment
Please, Sign In to add comment