Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. Button loginBtn, registerBtn;
  3. EditText usernameET, passwordET;
  4. TextView forgotTV;
  5. String works = "";
  6.  
  7. String username, password;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13.  
  14. usernameET = (EditText) findViewById(R.id.usernameET);
  15. passwordET = (EditText) findViewById(R.id.passwordET);
  16.  
  17.  
  18. loginBtn = (Button) findViewById(R.id.loginBtn);
  19. loginBtn.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. username = usernameET.getText().toString();
  23. password = passwordET.getText().toString();
  24. String method = "login";
  25.  
  26. BackgroundTask backgroundTask = new
  27. BackgroundTask(MainActivity.this);
  28. backgroundTask.execute(method, username, password);
  29.  
  30. if(true) {
  31. Intent intent = new Intent(MainActivity.this, Home.class);
  32. startActivity(intent);
  33. }
  34.  
  35. }
  36. });
  37.  
  38. registerBtn = (Button) findViewById(R.id.registerBtn);
  39. registerBtn.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View view) {
  42. startActivity(new Intent(MainActivity.this, Register.class));
  43. }
  44. });
  45.  
  46. forgotTV = (TextView) findViewById(R.id.forgotBtn);
  47. forgotTV.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View view) {
  50. startActivity(new Intent(MainActivity.this,
  51. ForgotPassword.class));
  52. }
  53. });
  54. }
  55.  
  56. public class BackgroundTask extends AsyncTask<String, Void, String> {
  57. Context context;
  58. AlertDialog alertDialog;
  59.  
  60. BackgroundTask(Context ctx) {
  61. context = ctx;
  62. }
  63.  
  64. @Override
  65. protected String doInBackground(String... params) {
  66. String registrationURL = "http://192.168.0.15:8080/imo/register.php";
  67. String loginURL = "http://192.168.0.15:8080/imo/login.php";
  68. String postURL = "http://192.168.0.15:8080/imo/postbox.php";
  69.  
  70. String line = "";
  71. String result = "";
  72.  
  73.  
  74. String type = params[0];
  75.  
  76. if (type.equals("login")) {
  77. String username = params[1];
  78. String password = params[2];
  79.  
  80. try {
  81. URL url = new URL(loginURL);
  82. HttpURLConnection httpURLConnection = (HttpURLConnection)
  83. url.openConnection();
  84. httpURLConnection.setRequestMethod("POST");
  85. httpURLConnection.setDoOutput(true);
  86. httpURLConnection.setDoInput(true);
  87. OutputStream OS = httpURLConnection.getOutputStream();
  88. BufferedWriter writer = new BufferedWriter(new
  89. OutputStreamWriter(OS, "UTF-8"));
  90. String postData = URLEncoder.encode("username", "UTF-8") + "=" +
  91. URLEncoder.encode(username, "UTF-8") + "&"
  92. + URLEncoder.encode("password", "UTF-8") + "=" +
  93. URLEncoder.encode(password, "UTF-8");
  94. writer.write(postData);
  95. writer.flush();
  96. writer.close();
  97. OS.close();
  98. InputStream IS = httpURLConnection.getInputStream();
  99. BufferedReader reader = new BufferedReader(new
  100. InputStreamReader(IS, "iso-8859-1"));
  101. while ((line = reader.readLine()) != null) {
  102. result += line;
  103. }
  104. reader.close();
  105. IS.close();
  106. httpURLConnection.disconnect();
  107. return result;
  108.  
  109. } catch (MalformedURLException e) {
  110. e.printStackTrace();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114.  
  115. } else if (type.equals("register")) {
  116. String username = params[1];
  117. String password = params[2];
  118. String email = params[3];
  119.  
  120. try {
  121. URL url = new URL(registrationURL);
  122. HttpURLConnection httpURLConnection = (HttpURLConnection)
  123. url.openConnection();
  124. httpURLConnection.setRequestMethod("POST");
  125. httpURLConnection.setDoOutput(true);
  126. httpURLConnection.setDoInput(true);
  127. OutputStream OS = httpURLConnection.getOutputStream();
  128. BufferedWriter writer = new BufferedWriter(new
  129. OutputStreamWriter(OS, "UTF-8"));
  130. String postData = URLEncoder.encode("username", "UTF-8") + "=" +
  131. URLEncoder.encode(username, "UTF-8") + "&"
  132. + URLEncoder.encode("password", "UTF-8") + "=" +
  133. URLEncoder.encode(password, "UTF-8") + "&"
  134. + URLEncoder.encode("email", "UTF-8") + "=" +
  135. URLEncoder.encode(email, "UTF-8");
  136. writer.write(postData);
  137. writer.flush();
  138. writer.close();
  139. OS.close();
  140. InputStream IS = httpURLConnection.getInputStream();
  141. BufferedReader reader = new BufferedReader(new
  142. InputStreamReader(IS, "iso-8859-1"));
  143. while ((line = reader.readLine()) != null) {
  144. result += line;
  145. }
  146. reader.close();
  147. IS.close();
  148. httpURLConnection.disconnect();
  149. return result;
  150.  
  151. } catch (MalformedURLException e) {
  152. e.printStackTrace();
  153. } catch (IOException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. else if(type.equals("postbox")){
  159. String body = params[1];
  160.  
  161. try {
  162. URL url = new URL(postURL);
  163. HttpURLConnection httpURLConnection = (HttpURLConnection)
  164. url.openConnection();
  165. httpURLConnection.setRequestMethod("POST");
  166. httpURLConnection.setDoOutput(true);
  167. httpURLConnection.setDoInput(true);
  168. OutputStream OS = httpURLConnection.getOutputStream();
  169. BufferedWriter writer = new BufferedWriter(new
  170. OutputStreamWriter(OS, "UTF-8"));
  171. String postData = URLEncoder.encode("body", "UTF-8") + "=" +
  172. URLEncoder.encode(body, "UTF-8");
  173. writer.write(postData);
  174. writer.flush();
  175. writer.close();
  176. OS.close();
  177. InputStream IS = httpURLConnection.getInputStream();
  178. BufferedReader reader = new BufferedReader(new
  179. InputStreamReader(IS, "iso-8859-1"));
  180. while ((line = reader.readLine()) != null) {
  181. result += line;
  182. }
  183. reader.close();
  184. IS.close();
  185. httpURLConnection.disconnect();
  186. return result;
  187.  
  188. } catch (MalformedURLException e) {
  189. e.printStackTrace();
  190. } catch (IOException e) {
  191. e.printStackTrace();
  192. }
  193. }
  194.  
  195. return null;
  196. }
  197.  
  198.  
  199. @Override
  200. protected void onPreExecute () {
  201. alertDialog = new AlertDialog.Builder(context).create();
  202. alertDialog.setTitle("LOG");
  203. }
  204.  
  205. @Override
  206. protected void onPostExecute (String result){
  207. alertDialog.setMessage(result);
  208. alertDialog.show();
  209.  
  210. }
  211.  
  212. @Override
  213. protected void onProgressUpdate (Void...values){
  214. super.onProgressUpdate(values);
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement