Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7.  
  8. }
  9.  
  10. public void userReg(View view){
  11. startActivity(new Intent(this, Register.class));
  12.  
  13. }
  14.  
  15. public void userLogin(View view){
  16.  
  17. }
  18. }
  19.  
  20. public class Register extends Activity {
  21.  
  22. EditText etName, etUsername, etPassword;
  23. String name, user_name, user_pass;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_register);
  28.  
  29. etName = (EditText)findViewById(R.id.name);
  30. etUsername = (EditText)findViewById(R.id.new_user_name);
  31. etPassword = (EditText)findViewById(R.id.new_user_pass);
  32. }
  33.  
  34. public void userReg(View view){
  35.  
  36. name = etName.getText().toString();
  37. user_name = etUsername.getText().toString();
  38. user_pass = etPassword.getText().toString();
  39.  
  40. String method = "register";
  41. BackgroundTask backgroundTask = new BackgroundTask(this);
  42. backgroundTask.execute(method,name,user_name,user_pass);
  43. finish();
  44.  
  45. }
  46. }
  47.  
  48. public class BackgroundTask extends AsyncTask<String, Void, String> {
  49.  
  50. Context ctx;
  51. BackgroundTask(Context ctx){
  52. this.ctx = ctx;
  53. }
  54.  
  55. @Override
  56. protected void onPreExecute() {
  57. super.onPreExecute();
  58. }
  59.  
  60. @Override
  61. protected String doInBackground(String... params) {
  62.  
  63. String reg_url = "http://10.0.2.2/webapp/register.php";
  64. String login_url = "http://10.0.2.2/webapp/login.php";
  65. String method = params[0];
  66. if (method.equals("register")) {
  67. String name = params[1];
  68. String user_name = params[2];
  69. String user_pass = params[3];
  70. try {
  71. URL url = new URL(reg_url);
  72. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  73. httpURLConnection.setRequestMethod("POST");
  74. httpURLConnection.setDoOutput(true);
  75. //httpURLConnection.setDoInput(true);
  76. OutputStream OS = httpURLConnection.getOutputStream();
  77. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  78. String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
  79. URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
  80. URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
  81. bufferedWriter.write(data);
  82. bufferedWriter.flush();
  83. bufferedWriter.close();
  84. OS.close();
  85. InputStream IS = httpURLConnection.getInputStream();
  86. IS.close();
  87. //httpURLConnection.connect();
  88. httpURLConnection.disconnect();
  89. return "Registration Success...";
  90. } catch (MalformedURLException e) {
  91. e.printStackTrace();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. return null;
  97. }
  98.  
  99. @Override
  100. protected void onProgressUpdate(Void... values) {
  101. super.onProgressUpdate(values);
  102. }
  103.  
  104. @Override
  105. protected void onPostExecute(String result) {
  106. Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement