gpt4 book ai didi

android - 从改造 onResponse 返回数据

转载 作者:行者123 更新时间:2023-12-05 08:51:24 28 4
gpt4 key购买 nike

我正在做一个基本的 Android 应用程序,它通过 API 执行简单的登录。我需要返回接收到的数据,但改进的 onResponse 方法具有 void 返回类型。我的代码是:

package com.example.deberesloginjava.data;

import android.util.Log;

import com.example.deberesloginjava.data.APIServices.Post;
import com.example.deberesloginjava.data.model.LoggedInUser;

import java.io.IOException;

import com.example.deberesloginjava.data.APIServices.APIService;
import com.example.deberesloginjava.data.APIServices.ApiUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.content.ContentValues.TAG;

/**
* Class that handles authentication w/ login credentials and retrieves user information.
*/

public class LoginDataSource {

private APIService mAPIService;

public void getData(Callback<Result<LoggedInUser>> callback){
apiClient.getData().enqueue(callback);
}

public Result<LoggedInUser> login(String username, String password) {

try {

mAPIService = ApiUtils.getAPIService();

mAPIService.login(username, password, "password").enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if(response.isSuccessful()) {
LoggedInUser loggedInUser;
loggedInUser = new LoggedInUser(response.body().getUserName(),response.body().getUserName(), response.body().getAccess_token());
return new Result.Success<>(loggedInUser);
}
}



@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API.");
}
});

} catch (Exception e) {
return new Result.Error(new IOException("Error logging in", e));
}
}
}

我发现至少有两个这样的问题,并且都具有几乎相同的回调解决方案,但我无法在我的案例中复制它。

提前致谢。

最佳答案

您必须添加自定义回调来处理这种情况。你不能在 onResponse() 和 onFailure() 中返回任何东西

public interface CustomCallback {

void onSucess(Result<LoggedInUser> value);
void onFailure();
}

像这样更新你的方法:

public void login(String username, String password,CustomCallback customCallback) {

try {

mAPIService = ApiUtils.getAPIService();

mAPIService.login(username, password, "password").enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if(response.isSuccessful()) {
LoggedInUser loggedInUser;
loggedInUser = new LoggedInUser(response.body().getUserName(),response.body().getUserName(), response.body().getAccess_token());
customCallback.onSucess(new Result.Success<>(loggedInUser));
}
}


@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API.");
customCallback.onFailure();
}
});

} catch (Exception e) {
e.printStactTrace();
}
}

您必须像下面这样调用您的 api:

login("user name","passwor",new CustomCallback(){
@Override
public void onSucess(Result<LoggedInUser> value){
//do your success code here
}
@Override
public void onFailure(){
}

});

关于android - 从改造 onResponse 返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60004591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com