我如何在回调Retrofit Android中返回类

问题描述 投票:0回答:1

我想在回调函数中返回UserResponse,但我不知道该怎么办。

 private void getUser(String api_token){


      Call<UserResponse> call =  APIService.getUserByToken(api_token);

            call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
              //   userResponse = response.body();
                if(response.isSuccessful()) {

                    Log.d("GET API TOKEN USER MAIN", String.valueOf(response.body().getUser().getId()));
// RETURN Here UserResponse
                }
            }
            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.d("Falloooooo", t.getMessage());
            }
        });


    }

android retrofit asynccallback
1个回答
0
投票

我希望了解您想要的内容

 public void getUser(String api_token) {
        CheckInternetConnection connectionCheck = new CheckInternetConnection(getContext());

        if (connectionCheck.haveNetworkConnection()) {
            ApiInterface apiService =
                    ApiClient.getClient().create(ApiInterface.class);

         Call<UserResponse> call = apiService.getUserByToken(api_token);;

            call.enqueue(new Callback<UserResponse>() {
                @Override
                public void onResponse(Call<UserResponse > call, Response<UserResponse > response) {

                    if (response.isSuccessful()) {

                      userName.setText(response.body().getName());

                    }
                }

                @Override
                public void onFailure(Call<UserResponse > call, Throwable t) {
                    Log.d("sucess", String.valueOf(t));

                }
            });
        } else {
            Toasty.info(getContext(), (getResources().getString(R.string.check_connection)), Toast.LENGTH_LONG).show();
        }
    }

和接口类

public interface ApiInterface {
 @GET("nameofapi")
    Call<UserResponse> getUserByToken(@Header("Access-Token") String access_token);
}
© www.soinside.com 2019 - 2024. All rights reserved.