Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package com.sleaq.php;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.os.Debug;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.net.HttpURLConnection;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28.  
  29. final EditText username = (EditText)findViewById(R.id.username);
  30. final EditText password = (EditText)findViewById(R.id.password);
  31.  
  32. Button button = (Button)findViewById(R.id.btnLogin);
  33. button.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. new Login().execute("http://www.sleaq.com/apps/416/login.php?username=" + username.getText().toString() + "&password=" + password.getText().toString());
  37. }
  38. });
  39. }
  40.  
  41. public class Login extends AsyncTask<String, String, String>
  42. {
  43. @Override
  44. protected String doInBackground(String... params)
  45. {
  46. HttpURLConnection conn = null;
  47.  
  48. try
  49. {
  50. URL url;
  51. url = new URL(params[0]);
  52. conn = (HttpURLConnection) url.openConnection();
  53. if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
  54. {
  55. InputStream is = conn.getInputStream();
  56. String response = convertStreamToString(is);
  57.  
  58. if (response == "User existiert")
  59. {
  60. //next Activity starten
  61. }
  62. else
  63. {
  64.  
  65. }
  66. }
  67. else
  68. {
  69.  
  70. }
  71. return "Done";
  72. }
  73. catch (MalformedURLException e)
  74. {
  75. e.printStackTrace();
  76. }
  77. catch (IOException e)
  78. {
  79. e.printStackTrace();
  80. }
  81. finally
  82. {
  83. if (conn != null)
  84. {
  85. conn.disconnect();
  86. }
  87. }
  88. return null;
  89. }
  90.  
  91. private String convertStreamToString(InputStream is)
  92. {
  93. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  94. StringBuilder sb = new StringBuilder();
  95.  
  96. String line = null;
  97. try
  98. {
  99. while ((line = reader.readLine()) != null)
  100. {
  101. sb.append(line).append('\n');
  102. }
  103. }
  104. catch (IOException e)
  105. {
  106. e.printStackTrace();
  107. }
  108. finally
  109. {
  110. try
  111. {
  112. is.close();
  113. }
  114. catch (IOException e)
  115. {
  116. e.printStackTrace();
  117. }
  118. }
  119. return sb.toString();
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement