Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. import android.app.AlertDialog;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.design.widget.Snackbar;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.Toolbar;
  10. import android.view.View;
  11.  
  12. import java.io.BufferedReader;
  13. import java.io.BufferedWriter;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.io.OutputStream;
  18. import java.io.OutputStreamWriter;
  19. import java.net.HttpURLConnection;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.net.URLEncoder;
  23.  
  24. import static android.support.v4.app.ActivityCompat.startActivity;
  25. import static android.support.v4.content.ContextCompat.startActivities;
  26.  
  27. /**
  28. * Created by Gonçalo on 23/03/2016.
  29. */
  30.  
  31. public class BackGroundWorker extends AsyncTask<String,Void,String> {
  32. Context context;
  33. AlertDialog alertDialog;
  34. BackGroundWorker (Context ctx) {
  35. context = ctx;
  36. }
  37.  
  38. @Override
  39. protected String doInBackground(String... params) {
  40. String type = params[0];
  41. String login_url = "http://192.168.1.79/login.php";
  42. String register_url = "http://192.168.1.79/register.php";
  43. //String verifyuserpass_url = "http://192.168.0.102/verifyuserpass.php";
  44. //String verifypass_url = "http://192.168.0.102/verifyuserpass.php";
  45. if(type.equals("login")) {
  46. try {
  47. String user_name = params[1];
  48. String password = params[2];
  49. URL url = new URL(login_url);
  50. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  51. httpURLConnection.setRequestMethod("POST");
  52. httpURLConnection.setDoOutput(true);
  53. httpURLConnection.setDoInput(true);
  54. OutputStream outputStream = httpURLConnection.getOutputStream();
  55. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  56. String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  57. String user = URLEncoder.encode(user_name, "UTF-8");//guarda o nome de utilizador introduzido
  58. String pass = URLEncoder.encode(password, "UTF-8");//guarda a pass introduzido
  59. System.err.println("------------------------------------------");
  60. /*/ if(user.equals("Nome de Utilizador")){
  61. if(pass.equals("Password")) {
  62. System.err.println("Entrou no if");
  63. startActivity(new Intent(this, Pagina1.class));}
  64.  
  65. bufferedWriter.write(post_data);
  66. bufferedWriter.flush();
  67. bufferedWriter.close();
  68. outputStream.close();
  69. InputStream inputStream = httpURLConnection.getInputStream();
  70.  
  71. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  72. String result="";
  73. String line="";
  74. while((line = bufferedReader.readLine())!= null) {
  75. result +=line;
  76.  
  77. }
  78.  
  79. bufferedReader.close();
  80. inputStream.close();
  81. httpURLConnection.disconnect();
  82. return result;
  83.  
  84. } catch (MalformedURLException e) {
  85. e.printStackTrace();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89.  
  90. } else if(type.equals("register")) {
  91. try {
  92. String name = params[1];
  93. String surname = params[2];
  94. String age = params[3];
  95. String username = params[4];
  96. String password = params[5];
  97. URL url = new URL(register_url);
  98. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  99. httpURLConnection.setRequestMethod("POST");
  100. httpURLConnection.setDoOutput(true);
  101. httpURLConnection.setDoInput(true);
  102. OutputStream outputStream = httpURLConnection.getOutputStream();
  103. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  104. String post_data = URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"
  105. + URLEncoder.encode("surname", "UTF-8")+"="+URLEncoder.encode(surname, "UTF-8")+"&"
  106. + URLEncoder.encode("age", "UTF-8")+"="+URLEncoder.encode(age, "UTF-8")+"&"
  107. + URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
  108. +URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
  109.  
  110. bufferedWriter.write(post_data);
  111. bufferedWriter.flush();
  112. bufferedWriter.close();
  113. outputStream.close();
  114. InputStream inputStream = httpURLConnection.getInputStream();
  115. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  116. String result="";
  117. String line="";
  118. while((line = bufferedReader.readLine())!= null) {
  119. result += line;
  120.  
  121. }
  122. bufferedReader.close();
  123. inputStream.close();
  124. httpURLConnection.disconnect();
  125. return result;
  126. } catch (MalformedURLException e) {
  127. e.printStackTrace();
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. }
  131.  
  132. }
  133.  
  134. return null;
  135. }
  136.  
  137.  
  138.  
  139. @Override
  140. protected void onPreExecute() {
  141. alertDialog = new AlertDialog.Builder(context).create();
  142. alertDialog.setTitle("Login Status");
  143.  
  144. }
  145.  
  146. @Override
  147. protected void onPostExecute(String result) {
  148. alertDialog.setMessage(result);
  149. alertDialog.show();
  150. if (result.equals("Login Success"));
  151. Intent myIntent = new Intent(this, Pagina1.class); // Im facing problem here
  152. startActivity(myIntent); // Im facing problem here
  153.  
  154. }
  155.  
  156. @Override
  157. protected void onProgressUpdate(Void... values) {
  158. super.onProgressUpdate(values);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement