Handle response from the Rest service using Volley

Asked

Viewed 185 times

0

I’m developing an application in android studio in which consumes a service I created in Play Framework. I am learning now how to create and consume services, and I am having difficulties in handling response when logging into my application, whether or not to log in correctly, it always falls into the same ELSE condition as Onresponse, therefore does not validate 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 apk side this 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);
}

}
  • I’m seeing that in the condition you try to compare a string with an int. Try quoting: response.trim().equals("404")

  • I was able to solve as follows: if (Response.contains("Error")) {

  • If you managed to resolve "post" a response.

1 answer

0


Do so:

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 resolved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.