Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- public string ValidateAccount(string user, string password)
- {
- UserAccount account = new UserAccount();
- return account.Login(user, password);
- }
- public class SendPostRequest extends AsyncTask<String, Void, String>
- {
- String username = edt_user.getText().toString();
- String password = edt_pass.getText().toString();
- @Override
- protected void onPreExecute() {
- pb_bar.setVisibility(View.VISIBLE);
- }
- @Override
- protected String doInBackground(String... params) {
- HttpURLConnection conn = null;
- try
- {
- java.net.URL url = new URL("http://192.190.191.97:65/api/AuthenticateUser/ValidateAccount");
- JSONObject postDataParams = new JSONObject();
- postDataParams.put("user", username);
- postDataParams.put("password", password);
- Log.e("params", postDataParams.toString());
- conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(15000); /* milliseconds */
- conn.setConnectTimeout(15000); /* milliseconds */
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(getPostDataString(postDataParams));
- writer.flush();
- writer.close();
- os.close();
- int responseCode = conn.getResponseCode();
- if (responseCode == HttpsURLConnection.HTTP_OK)
- {
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- while ((line = in.readLine()) != null)
- {
- sb.append(line);
- break;
- }
- in.close();
- return sb.toString();
- }
- else
- {
- return new String("false : " + responseCode);
- }
- }
- catch (Exception e) {
- return new String("Exception: " + e.getMessage());
- }
- finally
- {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String r) {
- pb_bar.setVisibility(View.GONE);
- Toast.makeText(getApplicationContext(), r, Toast.LENGTH_LONG).show();
- }
- public String getPostDataString(JSONObject params) throws Exception
- {
- StringBuilder result = new StringBuilder();
- boolean first = true;
- Iterator<String> itr = params.keys();
- while (itr.hasNext())
- {
- String key = itr.next();
- Object value = params.get(key);
- if (first)
- first = false;
- else
- result.append("&");
- result.append(URLEncoder.encode(key, "UTF-8"));
- result.append("=");
- result.append(URLEncoder.encode(value.toString(), "UTF-8"));
- }
- return result.toString();
- }
- }
Add Comment
Please, Sign In to add comment