I make authorization in my application, I encountered the problem that when I enter the correct username and password, authorization does not work, but simply goes to the next screen. After a POST request, I need to get a token in reply (for further manipulations in the application) or an error (incorrect login password), the receiving example should be as follows:

{ "success": true, "token": <token> } 

Help with the implementation of obtaining a token. Here is my POST implementation of the authorization request:

 public class LoginActivity extends AppCompatActivity { Context c; EditText ELogin; EditText passwordText; Button loginBtn; String password; String Login; String url = "http://smktesting.herokuapp.com/api/login/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login1); c = this; ELogin = (EditText) findViewById(R.id.input_login); passwordText = (EditText) findViewById(R.id.input_password); loginBtn = (Button) findViewById(R.id.btn_login); loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // _("Login button hit"); Login = ELogin.getText() + ""; password = passwordText.getText() + ""; if ( Login.length() == 0 || password.length() == 0) { Toast.makeText(c, "Please fill in all fields", Toast.LENGTH_SHORT).show(); return; } if ( Login.length() > 0 && password.length() > 0) { //Do networking Networking n = new Networking(); n.execute(url, Networking.NETWORK_STATE_lOGIN); Toast.makeText(c, "Login Done", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, Spisok.class); startActivity(intent); } } }); } public void onClickRegisters(View v){ Intent browserIntent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(browserIntent); } //AsyncTask good for long running tasks public class Networking extends AsyncTask { public static final int NETWORK_STATE_lOGIN = 1; @Override protected Object doInBackground(Object[] params) { getJson((String) params[0], (Integer) params[1]); return null; } } private void getJson(String url, int state) { //Do a HTTP POST, more secure than GET HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost(url); List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); boolean valid = false; switch (state) { case Networking.NETWORK_STATE_lOGIN: //Building key value pairs to be accessed on web postParameters.add(new BasicNameValuePair("username", Login)); postParameters.add(new BasicNameValuePair("password", password)); valid = true; break; default: Toast.makeText(c, "Unknown state", Toast.LENGTH_SHORT).show(); } if (valid == true) { //Reads everything that comes from server BufferedReader bufferedReader = null; StringBuffer stringBuffer = new StringBuffer(""); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters); request.setEntity(entity); //Send off to server HttpResponse response = httpClient.execute(request); //Reads response and gets content bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; String LineSeparator = System.getProperty("line.separator"); //Read back server output while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line + LineSeparator); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } decodeResultIntoJson(stringBuffer.toString()); } else { } } private void decodeResultIntoJson(String response) { if (response.contains("error")) { try { JSONObject jo = new JSONObject(response); String error = jo.getString("error"); } catch (JSONException e) { e.printStackTrace(); } } try { JSONObject jo = new JSONObject(response); String success = jo.getString("success"); String message = jo.getString("message"); } catch (JSONException e) { e.printStackTrace(); } } } 

    1 answer 1

    Now you have a request and, regardless of its results, a new activation is opened. You need to launch activation only if the result of the query is success = true and the token came back:

     private void decodeResultIntoJson(String response) { Boolean success; if (response.contains("success")) { try { JSONObject jo = new JSONObject(response); success= jo.getBoolean("success"); } catch (JSONException e) { e.printStackTrace(); } if(succes){ try { JSONObject jo = new JSONObject(response); String token= jo.getString("token"); //здесь записываешь token куда-либо или просто передаешь через интент в другое активити Toast.makeText(c, "Login Done", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, Spisok.class); intent.putExtra("token", token); startActivity(intent); } catch (JSONException e) { e.printStackTrace(); } } else{ //сообщаешь об ошибке удобным способом (алерт, тост или как пожелаешь) try { JSONObject jo = new JSONObject(response); String message = jo.getString("message "); Toast.makeText(this, message , Toast.LENGTH_SHORT).show(); } catch (JSONException e) { e.printStackTrace(); } } } } 
    • It seems to work, the only "but" application crashes if you do not note the lines: Toast.makeText (c, "Login Done", Toast.LENGTH_SHORT) .show (); Toast.makeText (this, message, Toast.LENGTH_SHORT) .show (); And is it possible to check whether the value is written to the variable token? there is just a suspicion that it passes an empty token to the next activites. - Crozen93
    • Well, because the first parameter needs to pass a context to the makeText method, so the first parameters need to be replaced with LoginActivity.this. It's easy to check - in the second activation: String token = getIntent (). GetExtras (). GetString ("token"). and either toggle the variable token to the log, either to the screen or to the same Toast. Well, either in the debug editor, step by step, see the variables. - Nikotin N
    • Replaced as you said turned out lines: Toast.makeText (LoginActivity.this, "Login Done", Toast.LENGTH_SHORT) .show (); Toast.makeText (LoginActivity.this, message, Toast.LENGTH_SHORT) .show (); But the application is still the same when you enter the correct or incorrect password login crashes. On the account of the token, he checked it all the same empty, then the problem is no longer with me but with the server? - Crozen93
    • I can't say anything to the crash account, but I don’t see the log)) but to the token account - either the request to the server is not correct or something else. You can throw the project to the post office (bznikita95@yandex.ru) - I'll take a look. - Nikotin N
    • Sent check mail. - Crozen93