如何在其他地方使用Retrofit响应

时间:2017-03-15 10:19:28

标签: android retrofit

我在我的应用程序中使用Retrofit并从我的服务器接收数据。 我在Retrofit的responseBody中从我的服务器接收数据,但是当我想使用这些接收的数据时,我的数组是空的?!!

这是我的班级:

public class ActivityApplicationsList extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_applications_list);

    Log.i(TAG , "size of response array: " +  populateDataFromServer().size())
    //this size is 0 but have to be 4 !
  }

  private ArrayList<StructApplication> populateDataFromServer() {

    final ArrayList<StructApplication> mine = new ArrayList<>();
    final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
    Call<ArrayList<AppModel>> call = service.getApp();

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

          ArrayList<AppModel> newAppModel = response.body();

          for(int i=0 ; i < newAppModel.size();i++){

            StructApplication structApplication = new StructApplication();
            structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
            structApplication.setId(newAppModel.get(i).getAppId());
            structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
            structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());

            mine.add(structApplication);
          }
      }

      @Override
      public void onFailure(Call<ArrayList<AppModel>> call, Throwable t) {
      }

    });

    return mine;
  }
}

我调试以确保在onResponse中收到的所有数据: my correct response from server

正如你所看到的,我正确地收到了所有数据,但是当我在这个类的onCreate中使用这个响应时,它是空的!

我真的很感激你对这个奇怪问题的回答。

4 个答案:

答案 0 :(得分:2)

这是因为您在响应到来之前打印列表大小。由于请求是异步发送的,并且您正在尝试在onResponse()回调方法之前获取大小。 尝试添加此行

Log.i(TAG , "size of response array: " +  populateDataFromServer().size())
<{1>}在onResponse() mine.add(structApplication);之后,您会看到正确的尺寸。

答案 1 :(得分:1)

首先返回并稍后执行,尝试这种方式......

 private ArrayList<StructApplication> populateDataFromServer() {

        final ArrayList<StructApplication> mine = new ArrayList<>();
        final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
        Call<ArrayList<AppModel>> call = service.getApp();

        Response<ArrayList<AppModel>> response = responseCall.execute();

        ArrayList<AppModel> newAppModel = response.body();

              for(int i=0 ; i < newAppModel.size();i++){

               StructApplication structApplication = new StructApplication();
               structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
               structApplication.setId(newAppModel.get(i).getAppId());
               structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
               structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());

                mine.add(structApplication);
              }

       return mine;


      }

答案 2 :(得分:1)

由于api调用而发生这种情况,因为api需要几秒钟才能获得响应,同时您返回了mine数组。所以,一旦从服务器获得价值,请返回响应。

这样做

为此类创建一个全局实例,如

public class ActivityApplicationsList extends Activity {
ArrayList<StructApplication> mine = new ArrayList();

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_applications_list);

     populateDataFromServer();

    Log.i(TAG , "size of response array: " +  mine.size());
  }




private void populateDataFromServer() {

    final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");
    Call<ArrayList<AppModel>> call = service.getApp();

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

          ArrayList<AppModel> newAppModel = response.body();

          for(int i=0 ; i < newAppModel.size();i++){

           StructApplication structApplication = new StructApplication();
           structApplication.setName(String.valueOf(newAppModel.get(i).getAppId()));
           structApplication.setId(newAppModel.get(i).getAppId());
           structApplication.setAppVersionReleaseDate(newAppModel.get(i).getAppVersionReDate());
           structApplication.setAppDeleted(newAppModel.get(i).getAppDeleted());

            mine.add(structApplication);
          }
      }

      @Override
      public void onFailure(Call<ArrayList<AppModel>> call, Throwable t) {
      }

    });    
  }
}

答案 3 :(得分:0)

您需要在请求后获得响应。参见代码注释

setContentView(R.layout.activity_main);

建议,制作这个构造函数

public class ActivityApplicationsList extends Activity 
    implements Callback<ArrayList<AppModel>> { // Implement callback here

    // These are final, so make them fields
    final ArrayList<StructApplication> mine = new ArrayList<>();
    final APIService service = ServiceGenerator.createService(APIService.class, "2015-03-01 14:26:00", "123456", "123456");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applications_list);

        populateDataFromServer();
    }

    // Callback implementation
    @Override
    public void onResponse(Call<ArrayList<AppModel>> call, Response<ArrayList<AppModel>> response) {

        final ArrayList<AppModel> responseBody = response.body();

        for(AppModel model : responseBody){

            StructApplication structApplication = new StructApplication();
            structApplication.setName(String.valueOf(model.getAppId()));
            structApplication.setId(model.getAppId());
            structApplication.setAppVersionReleaseDate(model.getAppVersionReDate());
            structApplication.setAppDeleted(model.getAppDeleted());

            mine.add(structApplication);
      }

      // adapter.notifyDataSetChanged(); // Need this if using ListView
      Log.d("SIZE", ""+mine.size()); // Correct size
  }

  @Override
  public void onFailure(Call<ArrayList<AppModel>> call, Throwable t) {
    // error
  }

  private void populateDataFromServer() { // this is void; it can't return
      service.getApp().enqueue(ActivityApplicationsList.this);
  }

然后,该循环可以简单地

public class StructApplication {
    public StructApplication(AppModel model) {
        setName(String.valueOf(model.getAppId());
        setId(model.getAppId());
        setAppDeleted(model.getAppDeleted());
        setAppVersionReleaseDate(model.getAppVersionReDate());
    }
}