【问题标题】:Scope issues in Android using Retrofit使用 Retrofit 的 Android 范围问题
【发布时间】:2015-06-27 14:43:21
【问题描述】:

我对使用 Retrofit 的 Android 中的变量范围有疑问:

在 MainActivity 中,我使用 Retrofit 将 JSON 回复获取到 POJO (ApiResponse),创建一个 extendedJourney 对象并将其添加到 extendedJourneyArrayList:

public class MainActivity extends Activity {        
    private ArrayList<ExtendedJourney> extendedJourneyArrayList = new ArrayList<>();
    ...

    getAPIReply(...){
        service.getInfo(..., new getCallback());
    ...}

    private class getCallback implements Callback<ApiResponse> {
        public void success(ApiResponse apiResponse, Response response) {
            try {                    
                consumeApiData(apiResponse);
                }
        ...
        }
    }
    private void consumeApiData(ApiResponse apiResponse){
        ExtendedJourney extendedJourney = new ExtendedJourney(apiResponse, params);
        extendedJourneyArrayList.add(extendedJourney);
    }
    public void getData(View view){
        getAPIReply(...);
        //Do stuff with the extendedJourneyArrayList  
    }

consumeApiData() 内部一切正常,即从 apiResponse 和其他参数正确创建了 extendedJourney 对象,并且用新的 extendedJourney 正确更新了 extendedJourneyArrayList。

但是,在 getData(View view) 中,extendedJourneyArrayList 是空的。

如何解决?谢谢:D

【问题讨论】:

    标签: java android callback retrofit


    【解决方案1】:

    您正在进行异步调用。
    这意味着,在调用 service.getInfo(..., new getCallback()); 之后,流程会继续正常进行,直到被回调中断。 因此,您在 getData(View v) 中的代码可能在收到响应之前正在执行。

    所以你应该对 callback 上的数据做你想做的事(例如在数据添加到 consumeApiData(..) 的末尾list ),或者做一个同步请求(你必须在一个单独的线程中做)。

    【讨论】:

      【解决方案2】:

      感谢@Kushtrim 的回答。为了解决我使用 AsyncTask 执行同步请求的问题,代码现在如下所示:

      public class MainActivity extends Activity {        
          private ArrayList<ExtendedJourney> extendedJourneyArrayList = new ArrayList<>();
      ...
          public void getData(View view){
              for(int i = 0; i < NUM_REQUESTS; i++){
                  new getAPIReply().execute(params);
              }
      
          }
      
          private class getAPIReply extends AsyncTask<Params, Void, ApiResponse>{
              @Override
              protected ApiResponse doInBackground(Coords[] coords) {
                  return service.getRouteInfo(params);
              }
              @Override
              protected void onPostExecute(ApiResponse apiResponse){
                  try {
                      consumeApiData(apiResponse);
                  } catch (JSONException e) {...}           
          } 
      
          private void consumeApiData(ApiResponse apiResponse) throws JSONException{
              ExtendedJourney extendedJourney = new ExtendedJourney(apiResponse, params);
              extendedJourneyArrayList.add(extendedJourney);
              if(extendedJourneyArrayList.size() == NUM_REQUESTS) {
                    //Do stuff
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-07-17
        • 1970-01-01
        • 2014-10-31
        • 2018-12-01
        • 2014-05-31
        • 2017-04-08
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        相关资源
        最近更新 更多