Return 改造 onResponse 的数据
Return data from retrofit onResponse
我正在做一个基本的 android 应用程序,它可以再次执行简单的登录 API。我需要 return 接收到的数据,但改进的 onResponse 方法具有 void return 类型。我的代码是:
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));
}
}
}
我发现至少有两个这样的问题,并且都具有几乎相同的回调解决方案,但我无法在我的案例中复制它。
提前致谢。
实际上你不能 return 一个值,相反你可以尝试在 DTO 中为 passing/storing 数据创建一个方法并在你的 respose.IsSuccessful
中调用该方法。将响应对象 json 传递给用于存储所需内容的方法,例如令牌、用户名 n 等。
你可以试试
if(response.isSuccessful()) {
// just a loader untill all operations are done
progressBar.SetVisibility(View.Visible);
//Make a toast here for successful login
LoggedInUser loggedInUser;
loggedInUser = new LoggedInUser(response.body().getUserName(),response.body().getUserName(), response.body().getAccess_token());
UserDTO(loggedInUser);//it will save the users login details.
}
else{
//Make a toast as per your responce error.code as Invalid Password or any other error
}
在UserDTO()中你可以为用户创建一个登录会话,当用户下次使用该应用程序时自动登录,他不需要再次输入ID和密码。
完成所有处理后导航到您想要的视图 StartActivity()
希望这就是您要实现的目标。
您必须添加自定义回调来处理这种情况。你不能 return 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 应用程序,它可以再次执行简单的登录 API。我需要 return 接收到的数据,但改进的 onResponse 方法具有 void return 类型。我的代码是:
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));
}
}
}
我发现至少有两个这样的问题,并且都具有几乎相同的回调解决方案,但我无法在我的案例中复制它。
提前致谢。
实际上你不能 return 一个值,相反你可以尝试在 DTO 中为 passing/storing 数据创建一个方法并在你的 respose.IsSuccessful
中调用该方法。将响应对象 json 传递给用于存储所需内容的方法,例如令牌、用户名 n 等。
你可以试试
if(response.isSuccessful()) {
// just a loader untill all operations are done
progressBar.SetVisibility(View.Visible);
//Make a toast here for successful login
LoggedInUser loggedInUser;
loggedInUser = new LoggedInUser(response.body().getUserName(),response.body().getUserName(), response.body().getAccess_token());
UserDTO(loggedInUser);//it will save the users login details.
}
else{
//Make a toast as per your responce error.code as Invalid Password or any other error
}
在UserDTO()中你可以为用户创建一个登录会话,当用户下次使用该应用程序时自动登录,他不需要再次输入ID和密码。
完成所有处理后导航到您想要的视图 StartActivity()
希望这就是您要实现的目标。
您必须添加自定义回调来处理这种情况。你不能 return 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(){
}
});