将Retrofit2响应传递给活动

时间:2018-06-16 06:17:43

标签: android oop callback retrofit

这是杀死我的眼睛,可能是你会看到的第一件事。但是太久了,我试图理解为什么这不起作用。所以这就是问题所在:

我使用Retrofit 2获取对象'Userlogs'。一切正常。 当我在Log中打印Response.body()时,对象对象似乎被完美地解析了。

然后我将此对象设置为我的类'RepoUser_log'的字段,以便在需要时获取对象。 当我想要它时,你会问? ......很简单,我想在mainActivity中使用它。

让我们来看看代码:

public class RepoUser_log {

    private static APIService userService = ApiUtils.getAPIService();
    public   Userslogs userslogs = new Userslogs();
    public  List<Userslogs> listUserslogs = new ArrayList<>();

    public RepoUser_log(int IdPersonne){
        userslogs = CallBy(IdPersonne);
    }
    public Userslogs  CallBy(int iduser){
    Call<Userslogs> call = userService.GetUser(iduser);
    call.enqueue(new Callback<Userslogs>() {

      @Override
    public void onResponse(Call<Userslogs> call, Response<Userslogs> response) {
   Userslogs   userslogs = (Userslogs) response.body();
   Log.d( "onResponse: " , response.body().getEmail()); // works well here !
   Log.d("usersLog : ", userslogs.getEmail());  // that too Works well !  So my object exists and is well parsed
 }

      @Override
  public void onFailure(Call<Userslogs> call, Throwable t) {
              Userslogs  userslogs = null;
           }
       });
        return userslogs;
    }
}

现在,当我想使用对象'us​​erlogs'时,我的MainActivity

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, NiveauFragment.OnMessageSendlistener{

    Userslogs userslogs;
    Joueur joueur;
    Personne personne;
    private View mProgressView;
    private static final String DATA_ID_PERSONNE = "idPersonne";
    int idPersonne;
    ServiceLocalisation serviceLocalisation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        idPersonne = intent.getIntExtra(DATA_ID_PERSONNE, idPersonne);

        RepoUser_log repoUser = new RepoUser_log(idPersonne);

        Log.d("doInBackground: ", repoUser.userslogs.getEmail()); // it fails here my object is null says the debugger

所以对我来说,似乎我的oop理解对retrofit2来说不是一个问题。 但是函数CallBy返回一个无法传递函数'void onResponse'的首字母的用户日志对象是一件新事....

有人可以帮助我...因为我还有许多其他东西可以从改造中得到:)我不想从活动本身加载。

谢谢。

2 个答案:

答案 0 :(得分:0)

您正在onResponse内创建新对象,在那里使用全局变量:

onResponse(Call<Userslogs> call, Response<Userslogs> response) {
   Userslogs   userslogs = (Userslogs) response.body();
   Log.d( "onResponse: " , response.body().getEmail()); // works well here !
   Log.d("usersLog : ", userslogs.getEmail());  // that too Works well !  So my object exists and is well parsed
 }

改变:

Userslogs userslogs = (Userslogs) response.body();

userslogs = = (Userslogs) response.body();

它应该有用。

答案 1 :(得分:0)

正如我上面评论的那样,你的实现是错误的, 所以我有一个解决方案

public class RepoUser_log {

    private static APIService userService = ApiUtils.getAPIService();
    public List<Userslogs> listUserslogs = new ArrayList<>();
    public ApiListenner listenner;
    private int IdPersonne;

    public RepoUser_log(int IdPersonne) {
        this.IdPersonne = IdPersonne;
    }

    public void setListenner(ApiListenner listenner) {
        this.listenner = listenner;
    }

    public void callApi() {
        CallBy(IdPersonne)
    }

    public Userslogs CallBy(int iduser) {
        Call<Userslogs> call = userService.GetUser(iduser);
        call.enqueue(new Callback<Userslogs>() {

            @Override
            public void onResponse(Call<Userslogs> call, Response<Userslogs> response) {
                if (listenner != null) listenner.onSuccess((Userslogs) response.body());
                Log.d("onResponse: ", response.body().getEmail()); // works well here !
                Log.d("usersLog : ",
                        userslogs.getEmail());  // that too Works well !  So my object exists and is well parsed
            }

            @Override
            public void onFailure(Call<Userslogs> call, Throwable t) {
                if (listenner != null) listenner.onFail(t);
            }
        });
        return userslogs;
    }

    public interface ApiListenner {
        void onSuccess(Userslogs userslogs);

        void onFail(Throwable t);
    }
}

在您的活动中

  @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        idPersonne = intent.getIntExtra(DATA_ID_PERSONNE, idPersonne);

        RepoUser_log repoUser = new RepoUser_log(idPersonne);

        repoUser.setListenner(new ApiListenner() {
            @Override
            public void onSuccess(Userslogs userslogs) {
                Log.d("doInBackground: ", userslogs.getEmail()); // it fails here my object is null says the debugger
            }

            @Override
            public void onFail(Throwable t) {
                // api fail
            }
        });
        repoUser.callApi(); 

    }