Im very noob at android. Im makin an android aplication with API Rest as well.
I know my back its working, i checked with postman and sends ma request get good:
@RequestMapping(value = "/login/{email}/{password}", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<Object> getLogin(HttpServletRequest request,
@PathVariable(value="email") String email,
@PathVariable(value="password") String pass) {
System.out.println("Peticion de login recibida");
Usuario user = usuarioService.login(email, pass);
return new ResponseEntity<Object>(user, HttpStatus.OK);
}
But now im trying to configurate my front with Android Studio. I made : ApiClient.java
public class ApiClient {
private static final String API_BASE_URL ="http://XXX.XXX.XXX.XXX:8080/";
private static Retrofit retrofit = null;
private static Gson gson;
public static Retrofit getClient() {
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Also the InterfaceApi.java I guess the problem its here, but im not sure:
public interface InterfaceAPI {
@FormUrlEncoded
@GET("login/{email}/{password}")
Call<Usuario> login(
@Path("email") String email,
@Path("password") String password
);
}
Also at the LoginActivity.java :
public class LoginActivity extends AppCompatActivity {
InterfaceAPI apiInterface;
private EditText editTextEmail;
private EditText editTextPassword;
private Button btnLogin;
private final String SALUDOS = "Hello from the other side";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextEmail = findViewById(R.id.editTextEmail);
editTextPassword = findViewById(R.id.editTextPassword);
btnLogin = findViewById(R.id.buttonLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Vamos a acceder a la pagina de inicio si se ha logeuado correctamente
// a traves de este btn y mandamos datos del usuario
userLogin();
}
});
}
private void userLogin() {
apiInterface = ApiClient.getClient().create(InterfaceAPI.class);
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if(checkValidaciones(email, password)){ // these are only checkings and they work
Call<Usuario> call = apiInterface.login(email, password);
call.enqueue(new Callback<Usuario>() {
@Override
public void onResponse(Call<Usuario> call, Response<Usuario> response) {
Usuario res = response.body();
if (res != null){
// vamos a la pagina de Inicial
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("saludo", SALUDOS);
//LANZAMOS
startActivity(intent);
Toast.makeText(LoginActivity.this, "Usuario correctamente logueado", Toast.LENGTH_LONG).show();
}else{
//nos quedariamos en la misma pagina de login
Toast.makeText(LoginActivity.this, "Error en login", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Usuario> call, Throwable t) {
//nos quedamos en la misma pagina de login
Toast.makeText(LoginActivity.this, "Error request", Toast.LENGTH_LONG).show();
}
});
}
}
}
The application just closes when i hit the button that i catch at de loginActivity... Can anyone help me please! Thank u in advance <3
If you add @FromUrlEncoded to the top of @GET, you will have java.lang.IllegalArgumentException: FormUrlEncoded can only be
specified on HTTP methods with request body (e.g., @POST).
Try removing @FormUrlEncoded from the method
Check if you have INTERNET permission in the manifest
Try after adding android:usesCleartextTraffic="true"
in application tag inside Manifest
Ty! yes i removed it, now it doesnt crash the app, but, still not making the connection with the back.
add that HttpLoggingInterceptor from other answer you will get more details from Logs
YES! it was that! i guess im not very familiar with android and i miss that permission!! ty
That's very common mistake in Android development.