2016-03-09 39 views
3

响应数据这是我的代码:如何保存在回调

public class TopicsFragment extends Fragment { 
    TopicList topicList; 
    List<Map<String, Object>> topics; 
    TopicsAdapter adapter; 
    FragmentActivity listener; 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof Activity) { 
      this.listener = (FragmentActivity) context; 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     RubyChinaService service = 
      RubyChinaApi.getInstance().getRubyChinaService(); 

     //Create a call instance for looking up Retrofit topics. 
     Call<TopicList> call = service.getTopics(); 
     call.enqueue(new Callback<TopicList>() { 
      @Override 
      public void onResponse(Call<TopicList> call, 
            Response<TopicList> response) { 
       topicList = response.body(); 
       topics = DataUtil.ObjectToMapList(topicList); 
       Log.i("debug", topicList.getTopics().get(0).title); 
      } 

      @Override 
      public void onFailure(Call<TopicList> call, Throwable t) { 
       t.printStackTrace(); 
      } 
     }); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
     adapter = new TopicsAdapter(listener, topics, R.layout.item_topic, 
      new String[]{ 
       "authorIcon", 
       "topicTitle", 
       "topicAuthor", 
       "repliesCount" 
      }, 
      new int[]{ 
       R.id.authorIcon, 
       R.id.topicTitle, 
       R.id.topicAuthor, 
       R.id.repliesCount 
      } 
     ); 
     return inflater.inflate(R.layout.fragment_topics, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     ListView lv = (ListView) view.findViewById(R.id.lvItems); 
     lv.setAdapter(adapter); 
    } 
} 

我是新来使用改造。我想保存Retrofit response.body(),但是当我用断点调试时,topics得到了null。什么是保存响应数据的正确方法?

回答

0

确保响应真的有效。您可以使用response.isSuccess()response.isSuccessful()进行检查,具体取决于您正在使用的Retrofit的版本。如果失败,请查看可通过response.raw().code()访问的HTTP状态代码。