Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. EditText userName , password;
  3. String res = "toomy";
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. userName= (EditText)findViewById(R.id.userName);
  11. password= (EditText)findViewById(R.id.password);
  12. }
  13.  
  14. public void login(View view) {
  15. String name = userName.getText().toString();
  16. String pass = password.getText().toString();
  17. String type="login";
  18.  
  19. BackgroundTask background= new BackgroundTask(this);
  20. background.execute(type, name, pass).toString();
  21. }
  22.  
  23. public class BackgroundTask extends AsyncTask<String,Void ,String> {
  24. AlertDialog alertDialog ;
  25. Context ctx;
  26.  
  27. BackgroundTask(Context ctx){
  28. this.ctx=ctx;
  29. }
  30. @Override
  31. protected void onPreExecute(){
  32. alertDialog= new AlertDialog.Builder(ctx).create();
  33. alertDialog.setTitle("Login information...");
  34. }
  35.  
  36. @Override
  37. protected String doInBackground(String... params) {
  38. String login_url="http://192.168.173.1/loginf.php";
  39. String method= params[0];
  40. if(method.equals("login")){
  41. String name = params[1];
  42. String password = params[2];
  43. try {
  44.  
  45. URL url= new URL(login_url);
  46. HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
  47. httpURLConnection.setRequestMethod("POST");
  48. httpURLConnection.setDoOutput(true);
  49. httpURLConnection.setDoInput(true);
  50. OutputStream outputStream = httpURLConnection.getOutputStream();
  51. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  52. String data = URLEncoder.encode("name","UTF-8") +"="+ URLEncoder.encode(name,"UTF-8")+"&"+
  53. URLEncoder.encode("password","UTF-8")+"="+ URLEncoder.encode(password,"UTF-8");
  54.  
  55. bufferedWriter.write(data);
  56. bufferedWriter.flush();
  57. bufferedWriter.close();
  58. outputStream.close();
  59.  
  60. InputStream inputStream = httpURLConnection.getInputStream();
  61. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  62. String response= "";
  63. String line= "";
  64. while ((line= bufferedReader.readLine())!= null){
  65. response+= line;
  66. }
  67. bufferedReader.close();
  68. inputStream.close();
  69. httpURLConnection.disconnect();
  70. res = response;
  71. Log.d("hgjh",res);
  72. return res;
  73. } catch (MalformedURLException e) {
  74. e.printStackTrace();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }//end of if
  79. return null;
  80. }//end of method doInBackground
  81.  
  82. @Override
  83. protected void onProgressUpdate(Void... values) {
  84. super.onProgressUpdate(values);
  85. }
  86.  
  87. @Override
  88. protected void onPostExecute(String result) {
  89. if (result.equals("Login success")) {
  90. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  91. Intent in = new Intent(getApplicationContext(), Main.class);
  92. startActivity(in);
  93. finish();
  94. }
  95. else {
  96. Toast.makeText(ctx, "login faild", Toast.LENGTH_LONG).show();
  97. }
  98. }
  99. }//end of class BackgroundTask
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement