Guest User

Untitled

a guest
Nov 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import android.content.Intent;
  2. import android.os.AsyncTask;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextWatcher;
  7. import android.view.View;
  8. import android.view.WindowManager;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import java.io.BufferedReader;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.net.HttpURLConnection;
  17. import java.net.URL;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20. private Button submit;
  21. private EditText loginLabel;
  22. private EditText passwordLabel;
  23. private TextView errorLabel;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. loginLabel = (EditText) findViewById(R.id.login);
  31. passwordLabel = (EditText) findViewById(R.id.password);
  32. errorLabel = (TextView) findViewById(R.id.errorLabel);
  33. submit = (Button) findViewById(R.id.button);
  34.  
  35. submit.setOnClickListener(
  36. new View.OnClickListener()
  37. {
  38. @Override
  39. public void onClick(View v) {
  40. String login = loginLabel.getText().toString();
  41. String password = passwordLabel.getText().toString();
  42.  
  43. // AsyncTask Execute
  44. Auth user = new Auth();
  45. user.execute(login, password);
  46. String token = user.getToken();
  47.  
  48. // При попытке вывести token через out.println - NullPointerException и остановка программы.
  49.  
  50. if (token != null && !token.trim().isEmpty()) {
  51. Intent intent = new Intent(v.getContext(), CourierActivity.class);
  52. intent.putExtra("token", token);
  53. startActivity(intent);
  54. } else {
  55. errorLabel.setText("Неправильный логин или пароль");
  56. }
  57. }
  58.  
  59. }
  60. }
  61. );
  62.  
  63. loginLabel.addTextChangedListener(
  64. new TextWatcher() {
  65. @Override
  66. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  67. public void onTextChanged(CharSequence s, int start, int before, int count) {}
  68. public void afterTextChanged(Editable s) {
  69. loginLabel.setError(null);
  70. }
  71. }
  72. );
  73. }
  74.  
  75. static class Auth extends AsyncTask<String, Void, String> {
  76. private String token;
  77. private String errorMessage;
  78.  
  79. @Override
  80. protected String doInBackground(String... params) {
  81. String query = "Здесь URL";
  82. HttpURLConnection connection = null;
  83. try {
  84. connection = (HttpURLConnection) new URL(query).openConnection();
  85. connection.setRequestMethod("POST");
  86. connection.setDoOutput(true);
  87. String urlParameters = "username=" + params[0] + "&password=" + params[1];
  88.  
  89. DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
  90. dataOutputStream.writeBytes(urlParameters);
  91. dataOutputStream.flush();
  92. dataOutputStream.close();
  93.  
  94. StringBuilder sb = new StringBuilder();
  95.  
  96. if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
  97. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  98. String line;
  99. while ((line = in.readLine()) != null) {
  100. sb.append(line);
  101. sb.append("n");
  102. }
  103.  
  104. return this.token = ServiceConnect.parse(sb.toString());
  105. } else {
  106. this.errorMessage = "fail: " + connection.getResponseCode() + ", " + connection.getResponseMessage();
  107. }
  108.  
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. }
  112.  
  113. return this.token = null;
  114. }
  115.  
  116. public String getToken() {
  117. return this.token;
  118. }
  119.  
  120. public String getErrorMessage() {
  121. return this.errorMessage;
  122. }
  123. }
  124. }
  125.  
  126. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  127. StrictMode.setThreadPolicy(policy);
Add Comment
Please, Sign In to add comment