Treat response from rest service using volley

0

I'm developing an application in android studio where it consumes a service I created in the Play Framework. I am now learning how to create and consume services, and I am having difficulty responding when I log in to my application, regardless of whether I log in correctly or not, it always falls under the same ONResponse ELSE condition, therefore it does not validate the login. The code below is from my login service:

public class Services extends Controller {

public static void login(String login, String senha) throws Exception {
    Agente agente = new Agente();
    agente.login = login;
    agente.senha = senha;

    if (agente.autenticar()) {
        agente = Agente.find("login = ?", agente.login).first();
        Gson json = new Gson();
        renderJSON(json.toJson(agente));

    } else {
        String mensagem = "Usuário ou senha incorreto";
        JsonObject j = new JsonObject();
        j.addProperty("Erro", 404);
        j.addProperty("msg", mensagem);
        renderJSON(j);
    }
}

and on the apk side this is my login class:

public class MainActivity extends AppCompatActivity {

public static final String LOGIN_URL = "http://192.168.0.108:9000/Services/login";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
private String username;
private String password;
private EditText editTextUsername;
private EditText editTextPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    editTextUsername = (EditText) findViewById(R.id.matricula);
    editTextPassword = (EditText) findViewById(R.id.senha);

    Button logar = (Button) findViewById( R.id.logar );


    logar.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            userLogin();
        }
    } );
}

public void userLogin(){
    username = editTextUsername.getText().toString().trim();
    password = editTextPassword.getText().toString().trim();

    StringRequest stringRequest = new StringRequest( Request.Method.POST, LOGIN_URL+"?login="+username + "&senha="+password, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response.trim().equals(404)) {
                Toast.makeText( MainActivity.this, response, Toast.LENGTH_LONG ).show();

            } else {//SEMPRE ENTRE NESSA CONDIÇÃO
                Intent intent = new Intent(MainActivity.this, PrincipalActivity.class);
                intent.putExtra(KEY_USERNAME, username);
                startActivity(intent);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText( MainActivity.this, error.toString(), Toast.LENGTH_LONG ).show();
        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError{
            Map<String, String> map = new HashMap<>();
            map.put(KEY_USERNAME, username);
            map.put(KEY_PASSWORD, password);
            return map;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

}
    
asked by anonymous 25.12.2017 / 15:32

1 answer

1

Do this:

if (response.contains("Erro")) {
      progressDialog.dismiss();
      Toast.makeText( DenunciaActivity.this, "Erro ao enviar", Toast.LENGTH_LONG ).show();

} else {
      progressDialog.dismiss();
      Intent intent = new Intent(DenunciaActivity.this, MainActivity.class);
      Toast.makeText( DenunciaActivity.this, "Enviado com sucesso!", Toast.LENGTH_LONG ).show();
}

Here it solved.

    
05.01.2018 / 16:12