I am using Android with java and Retrofit 2.9.0 with Gson converter. I have called a function login(email, password) which should return a Result. I want to make login with email and password using API build in laravel and also I want to use MVVM architecture with Android JAVA
public Result<LoggedInUser> login(String username, String password) {
try {
Call<LoggedInUser> call = RetrofitClient.getInstance().getApi().sendCredential(username, password);
call.enqueue(new Callback<LoggedInUser>() {
@Override
public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {
// I have get response here and can make object of LoggedInUser but how to return
// object to this function: "public Result<LoggedInUser> login(String username, String
//password)"
}
@Override
public void onFailure(Call<LoggedInUser> call, Throwable t) {
}
});
return new Result.Success<>(/* object created in on response */);
} catch (Exception e) {
return new Result.Error(new IOException("Error Logging In", e));
}
}
I also tried
public Result<LoggedInUser> login(String username, String password) {
LoggedInUser apiResponse;
try {
Call<LoggedInUser> call = RetrofitClient.getInstance().getApi().sendCredential(username, password);
Response<LoggedInUser> response = call.execute();
/* at this line i get this error and it move to exception:: D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true*/
apiResponse = response.body();
return new Result.Success<>(apiresponse);
} catch (Exception e) {
e.printStackTrace();
return new Result.Error(new IOException("Error Logging In", e));
}
}
here is the exception
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true W/System.err: android.os.NetworkOnMainThreadException W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:333) W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196) W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) W/System.err: at java.net.Socket.connect(Socket.java:605)
i have also add network configuration with domain. Actually my API is http build on laravel i have also try
android:usesCleartextTraffic="true"
also the name in api and used in my Application in same