Advertisement
Guest User

Untitled

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