Advertisement
Guest User

Untitled

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