I am making a POST call to a server running VS Console app using the Retrofit with OkHttpClient on an Android device (Android Studio app). I have the following setup for Login Activity:
LoginRequest loginRequest = new LoginRequest(username, password);
Call<LoginResponse> call = placeHolderApi.loginUser(loginRequest);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(@NonNull Call<LoginResponse> call, @NonNull Response<LoginResponse> response) {
if (response.isSuccessful()) {
LoginResponse loginResponse = response.body();
if (loginResponse != null && "success".equals(loginResponse.getStatus())) {
Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginActivity.this, "Error: " + response.message(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(@NonNull Call<LoginResponse> call, @NonNull Throwable t) {
Log.e("LoginActivity", "Authentication failed", t);
}
});
I am trying to handle this error:
Authentication failed java.net.SocketTimeoutException: timeout at okio.SocketAsyncTimeout.newTimeoutException(JvmOkio.kt:146)
at okio.AsyncTimeout.access$newTimeoutException(AsyncTimeout.kt:161)
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:339)
at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:430)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:323)
at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29) etc...
The VS app on server is correctly receiving the logginRequest from the Android device but the loop is always entering “onFailure”. Is there any solution ? I tried to send message from Server to Android app, it works so I don’t know why there is a network problem ? Thank you