Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. EditText username,password;
  2. String uname,pwd;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_login);
  8. username = (EditText) findViewById(R.id.username);
  9. password = (EditText) findViewById(R.id.password);
  10. }
  11.  
  12. public void invokeLogin(View view){
  13. uname = username.getText().toString();
  14. pwd = password.getText().toString();
  15. login(uname, pwd);
  16. }
  17. public void login(final String uname, String pwd) {
  18.  
  19. class loginAsync extends AsyncTask<String, Void, String> {
  20. private Dialog loadDialogue;
  21.  
  22. @Override
  23. protected void onPreExecute() {
  24. super.onPreExecute();
  25. loadDialogue = ProgressDialog.show(LoginActivity.this, "Please wait...", "Loading");
  26. }
  27.  
  28. @Override
  29. protected String doInBackground(String... params) {
  30. JSONObject jsonObject;
  31. String result="";
  32.  
  33. try {
  34.  
  35. URL url = new URL("https://localhost/Android App/Login.php");
  36. jsonObject = new JSONObject();
  37. jsonObject.put("uname", params[0]);
  38. jsonObject.put("pwd", params[1]);
  39. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  40. httpURLConnection.setReadTimeout(15000);
  41. httpURLConnection.setConnectTimeout(15000);
  42. httpURLConnection.setDoInput(true);
  43. httpURLConnection.setDoOutput(true);
  44. OutputStream outputStream = httpURLConnection.getOutputStream();
  45. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  46. writer.write(getPostDataString(jsonObject));
  47. writer.flush();
  48. writer.close();
  49. outputStream.close();
  50. int responsecode = httpURLConnection.getResponseCode();
  51.  
  52. if (responsecode == HttpURLConnection.HTTP_OK) {
  53.  
  54. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
  55. StringBuilder sb = new StringBuilder(" ");
  56. String line;
  57. while ((line = bufferedReader.readLine()) != null) {
  58.  
  59. sb.append(line);
  60. }
  61. bufferedReader.close();
  62. result = sb.toString();
  63. }
  64. else {
  65. return new String("false " + responsecode);
  66. }
  67.  
  68. }
  69. catch (NullPointerException e) {
  70. e.printStackTrace();
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. } catch (JSONException e) {
  74. e.printStackTrace();
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. return result;
  79. }
  80. @Override
  81. protected void onPostExecute(String result) {
  82. String s = result.trim();
  83. loadDialogue.dismiss();
  84. if(s.equalsIgnoreCase("success")){
  85. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  86. finish();
  87. startActivity(intent);
  88. }else {
  89. Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
  90. }
  91. }
  92. }
  93. }
  94. public String getPostDataString(JSONObject params) throws Exception {
  95. StringBuilder result = new StringBuilder();
  96. boolean first = true;
  97. Iterator<String> itr = params.keys();
  98. while (itr.hasNext()) {
  99. String key = itr.next();
  100. Object value = params.get(key);
  101. if (first)
  102. first = false;
  103. else {
  104. result.append("&");
  105. result.append(URLEncoder.encode(key, "UTF-8"));
  106. result.append("=");
  107. result.append(URLEncoder.encode(value.toString(), "UTF-8"));
  108. }
  109. }
  110. return result.toString();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement