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(); } } }