Advertisement
Guest User

Untitled

a guest
Apr 21st, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 KB | None | 0 0
  1. Button bttnLogin;
  2. EditText loginEmail, loginPassword;
  3. TextView regLink;
  4. AlertDialog.Builder alert;
  5.  
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState)
  9. {
  10.  
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_login);
  13.  
  14. loginEmail = (EditText) findViewById(R.id.email);
  15. loginPassword = (EditText) findViewById(R.id.password);
  16.  
  17. regLink = (TextView) findViewById(R.id.regLink);
  18. regLink.setOnClickListener(new View.OnClickListener()
  19. {
  20. @Override
  21. public void onClick(View v)
  22. {
  23. startActivity(new Intent(Login.this, Register.class));
  24.  
  25. }
  26. });
  27.  
  28.  
  29. bttnLogin = (Button) findViewById(R.id.bttnLogin);
  30. bttnLogin.setOnClickListener(new View.OnClickListener()
  31. {
  32.  
  33. @Override
  34. public void onClick(View v)
  35. {
  36. if (loginEmail.getText().toString().equals("") || loginPassword.getText().toString().equals(""))
  37. {
  38. alert = new AlertDialog.Builder(Login.this);
  39. alert.setTitle("Login Failed");
  40. alert.setMessage("Try again");
  41. alert.setPositiveButton("OK", new DialogInterface.OnClickListener()
  42. {
  43.  
  44. @Override
  45. public void onClick(DialogInterface dialog, int which)
  46. {
  47.  
  48. dialog.dismiss();
  49. }
  50.  
  51. });
  52.  
  53. AlertDialog alertDialog = alert.create();
  54. alertDialog.show();
  55. } else //if user provides proper data
  56. {
  57. BackgroundTask backgroundTask = new BackgroundTask(Login.this);
  58. backgroundTask.execute("login", loginEmail.getText().toString(), loginPassword.getText().toString());
  59. }
  60.  
  61. }
  62.  
  63.  
  64. });
  65.  
  66. private Button regButton;
  67. public EditText regName;
  68. public EditText regEmail;
  69. public EditText regPassword;
  70. public EditText conPassword;
  71. private AlertDialog.Builder alert;
  72.  
  73.  
  74.  
  75. @Override
  76. protected void onCreate(Bundle savedInstanceState)
  77. {
  78. super.onCreate(savedInstanceState);
  79. setContentView(R.layout.activity_register);
  80.  
  81. regName = (EditText) findViewById(R.id.name);
  82. regEmail = (EditText) findViewById(R.id.email);
  83. regPassword = (EditText) findViewById(R.id.password);
  84. conPassword = (EditText) findViewById(R.id.conPassword);
  85. regButton = (Button) findViewById(R.id.regButton);
  86. regButton.setOnClickListener(this);
  87. }
  88.  
  89.  
  90. @Override
  91. public void onClick(View v)
  92. {
  93.  
  94. if (regName.getText().toString().equals("") || regEmail.getText().toString().equals("") || regPassword.getText().toString().equals("") || conPassword.getText().toString().equals(""))
  95. {
  96. alert = new AlertDialog.Builder(Register.this);
  97. alert.setTitle("Something not quite right");
  98. alert.setMessage("Please fill in all the fields");
  99. alert.setPositiveButton("OK", new DialogInterface.OnClickListener()
  100. {
  101.  
  102. @Override
  103. public void onClick(DialogInterface dialog, int which)
  104. {
  105.  
  106. dialog.dismiss();
  107. }
  108.  
  109. });
  110.  
  111. AlertDialog alertDialog = alert.create();
  112. alertDialog.show();
  113.  
  114. } else if (!(regPassword.getText().toString().equals(conPassword.getText().toString())))
  115. {
  116.  
  117. alert = new AlertDialog.Builder(Register.this);
  118. alert.setTitle("Passwords do not match");
  119. alert.setMessage("Try Again");
  120. alert.setPositiveButton("OK", new DialogInterface.OnClickListener()
  121. {
  122.  
  123. @Override
  124. public void onClick(DialogInterface dialog, int which)
  125. {
  126.  
  127. dialog.dismiss();
  128.  
  129. conPassword.setText("");
  130. regPassword.setText("");
  131. }
  132.  
  133. });
  134.  
  135. AlertDialog alertDialog = alert.create();
  136. alertDialog.show();
  137.  
  138.  
  139. }
  140. else //if user provides proper data
  141. {
  142. BackgroundTask backgroundTask = new BackgroundTask(Register.this);
  143. backgroundTask.execute("register", regName.getText().toString(), regEmail.getText().toString()
  144. , regPassword.getText().toString(), conPassword.getText().toString());
  145. }
  146.  
  147. public class BackgroundTask extends AsyncTask<String,Void,String>
  148. {
  149.  
  150. private Context context;
  151. private Activity activity;
  152. private String reg_url = "http://blaah.com/register.php";
  153. private String login_url = "http://blaah.com/login.php";
  154. private AlertDialog.Builder builder;
  155. private ProgressDialog progressDialog;
  156.  
  157.  
  158. public BackgroundTask(Context context)
  159. {
  160.  
  161. this.context = context;
  162. activity = (Activity) context;
  163. }
  164.  
  165. @Override
  166. protected void onPreExecute()
  167. {
  168. builder = new AlertDialog.Builder(activity);
  169. progressDialog = new ProgressDialog(context);
  170. progressDialog.setTitle("Please wait");
  171. progressDialog.setMessage("Connecting to Server...");
  172. progressDialog.setIndeterminate(true);
  173. progressDialog.setCancelable(false);
  174. progressDialog.show();
  175. }
  176.  
  177. @Override
  178. protected String doInBackground(String... params)
  179. {
  180.  
  181. String method = params[0];
  182.  
  183. if (method.equals("register"))
  184. {
  185.  
  186. try
  187. {
  188. URL url = new URL(reg_url);
  189. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  190. httpURLConnection.setRequestMethod("POST");
  191. httpURLConnection.setDoOutput(true);
  192. httpURLConnection.setDoInput(true);
  193. OutputStream outputStream = httpURLConnection.getOutputStream();
  194. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  195. String name = params[1];
  196. String email = params[2];
  197. String username = params[3];
  198. String password = params[4];
  199. String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
  200. URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
  201. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  202. bufferedWriter.write(data);
  203. bufferedWriter.flush();
  204. bufferedWriter.close();
  205. outputStream.close();
  206. InputStream inputStream = httpURLConnection.getInputStream();
  207. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  208. StringBuilder stringBuilder = new StringBuilder();
  209. String line = "";
  210. while ((line = bufferedReader.readLine()) != null)
  211. {
  212.  
  213. stringBuilder.append(line).append("n");
  214.  
  215. }
  216.  
  217.  
  218. httpURLConnection.disconnect();
  219. Thread.sleep(8000);
  220. return stringBuilder.toString().trim();
  221.  
  222. } catch (MalformedURLException e)
  223. {
  224. e.printStackTrace();
  225. } catch (IOException e)
  226. {
  227. e.printStackTrace();
  228. } catch (InterruptedException e)
  229. {
  230. e.printStackTrace();
  231. }
  232.  
  233. }
  234. else if (method.equals("login"))
  235. {
  236. try
  237. {
  238. URL url = new URL(login_url);
  239. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  240. httpURLConnection.setRequestMethod("POST");
  241. httpURLConnection.setDoOutput(true);
  242. httpURLConnection.setDoInput(true);
  243. OutputStream outputStream = httpURLConnection.getOutputStream();
  244. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  245.  
  246.  
  247. String username,password;
  248.  
  249. username = params[1];
  250. password = params [2];
  251. String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  252. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  253. bufferedWriter.write(data);
  254. bufferedWriter.flush();
  255. bufferedWriter.close();
  256. outputStream.close();
  257.  
  258. InputStream inputStream = httpURLConnection.getInputStream();
  259. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  260. StringBuilder stringBuilder = new StringBuilder();
  261. String line = "";
  262. while ((line = bufferedReader.readLine()) != null)
  263. {
  264.  
  265. stringBuilder.append(line + "n");
  266.  
  267. }
  268.  
  269.  
  270. httpURLConnection.disconnect();
  271. Thread.sleep(5000);
  272. return stringBuilder.toString().trim();
  273.  
  274.  
  275. } catch (MalformedURLException e)
  276. {
  277. e.printStackTrace();
  278. } catch (ProtocolException e)
  279. {
  280. e.printStackTrace();
  281. } catch (UnsupportedEncodingException e)
  282. {
  283. e.printStackTrace();
  284. } catch (IOException e)
  285. {
  286. e.printStackTrace();
  287. } catch (InterruptedException e)
  288. {
  289. e.printStackTrace();
  290. }
  291. }
  292.  
  293. return null;
  294. }
  295.  
  296. @Override
  297. protected void onProgressUpdate(Void... values)
  298. {
  299. super.onProgressUpdate(values);
  300. }
  301.  
  302. @Override
  303. protected void onPostExecute(String json)
  304. {
  305. try
  306. {
  307.  
  308. progressDialog.dismiss();
  309.  
  310.  
  311. Log.v("JSON", json);
  312. JSONObject jsonObject = new JSONObject(json);
  313. JSONArray jsonArray = jsonObject.getJSONArray("server_response");
  314. JSONObject jsonobject = jsonArray.getJSONObject(0);
  315. String code = jsonobject.getString("code");
  316. String message = jsonobject.getString("message");
  317.  
  318.  
  319.  
  320. if(code.equals("reg_true"))
  321. {
  322. showDialog("Sucessful registration.Thank you.Enjoy_AS!", message, code);
  323. }
  324.  
  325. else if (code.equals("reg_false"))
  326. {
  327. showDialog("User Already exists", message, code);
  328. }
  329.  
  330. else if (code.equals("login_true"))
  331. {
  332. Toast.makeText(context, "You are logged in", Toast.LENGTH_LONG).show();
  333. Intent intent = new Intent(activity, SplashScreen.class);
  334. activity.startActivity(intent);
  335.  
  336.  
  337. } else if (code.equals("login_false"))
  338. {
  339. showDialog("Login Error", message, code);
  340. }
  341.  
  342.  
  343. } catch (JSONException e){
  344. e.printStackTrace();
  345. }
  346.  
  347.  
  348. }
  349.  
  350. public void showDialog(String title,String message,String code)
  351. {
  352.  
  353. builder.setTitle(title);
  354. if (code.equals("reg_true") || code.equals("reg_false"))
  355. {
  356. builder.setMessage(message);//message form server
  357. builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
  358.  
  359. {
  360. @Override
  361. public void onClick(DialogInterface dialog, int which)
  362. {
  363. dialog.dismiss();
  364. activity.finish();
  365.  
  366. }
  367. });
  368.  
  369.  
  370. }
  371.  
  372. else if (code.equals("login_false"))
  373. {
  374.  
  375.  
  376. builder.setMessage(message);
  377. builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
  378. {
  379. @Override
  380. public void onClick(DialogInterface dialog, int which)
  381. {
  382. EditText loginEmail, loginPassword;
  383. loginEmail = (EditText) activity.findViewById(R.id.email);
  384. loginPassword = (EditText) activity.findViewById(R.id.password);
  385. loginEmail.setText("");
  386. loginPassword.setText("");
  387. dialog.dismiss();
  388.  
  389. }
  390.  
  391.  
  392. });
  393.  
  394.  
  395. }
  396. AlertDialog alertDialog = builder.create();
  397. alertDialog.show();
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement