我如何在 OnResponse 函数之外使用 Retrofit 响应,以便我可以 return 使用推荐的架构模型?
How can I use the Retrofit response outside the OnResponse function so I can return it up the recommended architecture model?
这个问题是 How can I use the Retrofit response outside the OnResponse function? 的后续问题,但我不能发表评论,所以我在这里问自己。
我正在尝试使用 Android Studio 登录模板,因为它遵循推荐的体系结构,但我在 return 在 LoginDataSource.login 中输入结果时遇到问题。结果被困在Call.enqueue函数中,我无法将其取出到return。我已经复制了上面 link 中建议的回调,但这只是将结果捕获在新的 class.
中
如何访问由我的服务器 return 编辑到我的存储库的 LoggedInUser return?
原始尝试:用户卡在 Call.enqueue - onResponse
public class LoginDataSource {
public static Retrofit retrofit = null;
LoggedInUser user;
public Result<LoggedInUser> login(String username, String password) {
user = new LoggedInUser();
try {
// TODO: handle loggedInUser authentication
retrofit = new Retrofit.Builder()
.baseUrl("https://myserver")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostEndPoint endPoint = retrofit.create(PostEndPoint.class);
Call<LoggedInUser> call = endPoint.getUser("login", username, password);
call.enqueue(new Callback<LoggedInUser>() {
@Override
public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {
Log.d(TAG, "onResponse: code " + response.code());
if (response.isSuccessful()){
callback.getUser();
user = response.body();
Log.d(TAG, "onResponse: " + user); // this returns valid user data
}
}
});
Log.d(TAG, "retrofit complete:" + user.getName()); // this returns null
return new Result.Success<>(user);
}
}
}
执行回调后:用户卡在 GetUserResponse - getUser
public class LoginDataSource {
public static Retrofit retrofit = null;
LoggedInUser user;
public Result<LoggedInUser> login(String username, String password) {
user = new LoggedInUser();
try {
// TODO: handle loggedInUser authentication
retrofit = new Retrofit.Builder()
.baseUrl("https://myserver")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostEndPoint endPoint = retrofit.create(PostEndPoint.class);
Call<LoggedInUser> call = endPoint.getUser("login", username, password);
sendLoginRequest(call, new GetUserResponse(){
@Override
public void getUser(LoggedInUser userFromResponse) {
user = userFromResponse;
}
});
Log.d(TAG, "retrofit complete:" + user.getName()); // this returns null
return new Result.Success<>(user);
}
}
private void sendLoginRequest (Call call, final GetUserResponse callback) {
call.enqueue(new Callback<LoggedInUser>() {
@Override
public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {
if (response.isSuccessful()){
callback.getUser(response.body());
}
}
});
}
}
public interface GetUserResponse {
void getUser(LoggedInUser user);
}
我觉得我需要让用户发送登录请求 return,但我不知道该怎么做。我正朝着正确的方向前进吗?欢迎任何建议。
我一路切换到Kotlin,所以这可能不正确Java,但它显示了过程
- 设置用户模型为LiveData
- 使用 .postValue()
更新用户模型
- 在视图模型(未显示)中为用户模型设置 observer
public class LoginDataSource {
public static Retrofit retrofit = null;
MutableLiveData<LoggedInUser> user=new MutableLiveData<>(); // changed this to LiveData so it can be observed from the viewmodel
public Result<LoggedInUser> login(String username, String password) {
user = new LoggedInUser();
try {
// TODO: handle loggedInUser authentication
retrofit = new Retrofit.Builder()
.baseUrl("https://myserver")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostEndPoint endPoint = retrofit.create(PostEndPoint.class);
loggedInUser = endPoint.getUser("login", username, password);
user.postValue(loggedInUser); // updated the livedata here
}
}
}
这个问题是 How can I use the Retrofit response outside the OnResponse function? 的后续问题,但我不能发表评论,所以我在这里问自己。
我正在尝试使用 Android Studio 登录模板,因为它遵循推荐的体系结构,但我在 return 在 LoginDataSource.login 中输入结果时遇到问题。结果被困在Call.enqueue函数中,我无法将其取出到return。我已经复制了上面 link 中建议的回调,但这只是将结果捕获在新的 class.
中如何访问由我的服务器 return 编辑到我的存储库的 LoggedInUser return?
原始尝试:用户卡在 Call.enqueue - onResponse
public class LoginDataSource {
public static Retrofit retrofit = null;
LoggedInUser user;
public Result<LoggedInUser> login(String username, String password) {
user = new LoggedInUser();
try {
// TODO: handle loggedInUser authentication
retrofit = new Retrofit.Builder()
.baseUrl("https://myserver")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostEndPoint endPoint = retrofit.create(PostEndPoint.class);
Call<LoggedInUser> call = endPoint.getUser("login", username, password);
call.enqueue(new Callback<LoggedInUser>() {
@Override
public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {
Log.d(TAG, "onResponse: code " + response.code());
if (response.isSuccessful()){
callback.getUser();
user = response.body();
Log.d(TAG, "onResponse: " + user); // this returns valid user data
}
}
});
Log.d(TAG, "retrofit complete:" + user.getName()); // this returns null
return new Result.Success<>(user);
}
}
}
执行回调后:用户卡在 GetUserResponse - getUser
public class LoginDataSource {
public static Retrofit retrofit = null;
LoggedInUser user;
public Result<LoggedInUser> login(String username, String password) {
user = new LoggedInUser();
try {
// TODO: handle loggedInUser authentication
retrofit = new Retrofit.Builder()
.baseUrl("https://myserver")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostEndPoint endPoint = retrofit.create(PostEndPoint.class);
Call<LoggedInUser> call = endPoint.getUser("login", username, password);
sendLoginRequest(call, new GetUserResponse(){
@Override
public void getUser(LoggedInUser userFromResponse) {
user = userFromResponse;
}
});
Log.d(TAG, "retrofit complete:" + user.getName()); // this returns null
return new Result.Success<>(user);
}
}
private void sendLoginRequest (Call call, final GetUserResponse callback) {
call.enqueue(new Callback<LoggedInUser>() {
@Override
public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {
if (response.isSuccessful()){
callback.getUser(response.body());
}
}
});
}
}
public interface GetUserResponse {
void getUser(LoggedInUser user);
}
我觉得我需要让用户发送登录请求 return,但我不知道该怎么做。我正朝着正确的方向前进吗?欢迎任何建议。
我一路切换到Kotlin,所以这可能不正确Java,但它显示了过程
- 设置用户模型为LiveData
- 使用 .postValue() 更新用户模型
- 在视图模型(未显示)中为用户模型设置 observer
public class LoginDataSource {
public static Retrofit retrofit = null;
MutableLiveData<LoggedInUser> user=new MutableLiveData<>(); // changed this to LiveData so it can be observed from the viewmodel
public Result<LoggedInUser> login(String username, String password) {
user = new LoggedInUser();
try {
// TODO: handle loggedInUser authentication
retrofit = new Retrofit.Builder()
.baseUrl("https://myserver")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostEndPoint endPoint = retrofit.create(PostEndPoint.class);
loggedInUser = endPoint.getUser("login", username, password);
user.postValue(loggedInUser); // updated the livedata here
}
}
}