Advertisement
selvalives

Untitled

Aug 31st, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.nsu.visitorsys">
  3. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme"
  11. android:usesCleartextTraffic="true">
  12. <activity android:name=".loginactivity">
  13. <intent-filter >
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. <activity android:name=".menuactivity">
  19. </activity>
  20. </application>
  21. </manifest>
  22.  
  23. --inside loginactivity
  24. --declarations
  25. public static final int CONNECTION_TIMEOUT=10000;
  26. public static final int READ_TIMEOUT=150000;
  27.  
  28.  
  29. --put this inside loginactivity, inside the class ,outside the oncreate
  30. --change the ip address as well in the below code
  31.  
  32.  
  33. private class AsyncLogin extends AsyncTask<String, String, String>
  34. {
  35. ProgressDialog pdLoading = new ProgressDialog(loginactivity.this);
  36. HttpURLConnection conn;
  37. URL url = null;
  38.  
  39. @Override
  40. protected void onPreExecute()
  41. {
  42. super.onPreExecute();
  43. pdLoading.setMessage("\tLoading...");
  44. pdLoading.setCancelable(false);
  45. pdLoading.show();
  46. }
  47. @Override
  48. protected String doInBackground(String... params)
  49. {
  50. try
  51. {
  52. url = new URL("http://172.25.101.48/NUSVisitorWeb/login.php");
  53. }
  54. catch (MalformedURLException e)
  55. {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. return "exception";
  59. }
  60. try
  61. {
  62. conn = (HttpURLConnection)url.openConnection();
  63. conn.setReadTimeout(READ_TIMEOUT);
  64. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  65. conn.setRequestMethod("POST");
  66. conn.setDoInput(true);
  67. conn.setDoOutput(true);
  68. Uri.Builder builder = new Uri.Builder()
  69. .appendQueryParameter("username", params[0])
  70. .appendQueryParameter("password", params[1]);
  71. String query = builder.build().getEncodedQuery();
  72. OutputStream os = conn.getOutputStream();
  73. BufferedWriter writer = new BufferedWriter(
  74. new OutputStreamWriter(os, "UTF-8"));
  75. writer.write(query);
  76. writer.flush();
  77. writer.close();
  78. os.close();
  79. conn.connect();
  80. }
  81. catch (IOException e1)
  82. {
  83. e1.printStackTrace();
  84. return "exception";
  85. }
  86. try
  87. {
  88. int response_code = conn.getResponseCode();
  89. if (response_code == HttpURLConnection.HTTP_OK)
  90. {
  91. InputStream input = conn.getInputStream();
  92. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  93. StringBuilder result = new StringBuilder();
  94. String line;
  95.  
  96. while ((line = reader.readLine()) != null)
  97. {
  98. result.append(line);
  99. }
  100.  
  101. return(result.toString());
  102. }
  103. else
  104. {
  105. return("unsuccessful");
  106. }
  107.  
  108. } catch (IOException e)
  109. {
  110. e.printStackTrace();
  111. return "exception";
  112. }
  113. finally {
  114. conn.disconnect();
  115. }
  116. }
  117. @Override
  118. protected void onPostExecute(String result)
  119. {
  120. try
  121. {
  122. JSONObject json=new JSONObject(result);
  123. if(json.getString("status").equals("true"))
  124. {
  125.  
  126. Toast.makeText(loginactivity.this, "Login Success...", Toast.LENGTH_LONG).show();
  127. Intent intent = new Intent(loginactivity.this,menuactivity.class);
  128. startActivity(intent);
  129. loginactivity.this.finish();
  130. }
  131. else if(json.getString("status").equals("false"))
  132. {
  133. Toast.makeText(loginactivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
  134. }
  135. else
  136. {
  137. Toast.makeText(loginactivity.this, "Please check server connection", Toast.LENGTH_LONG).show();
  138. }
  139. }
  140. catch (JSONException e)
  141. {
  142. e.printStackTrace();
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement