使用Otto进行改造 - 订阅未调用

时间:2015-07-20 19:09:44

标签: android retrofit otto

我花了很多时间来解决这个问题,但仍然无法找到正确的方法。 希望有人可以提供帮助。

场合

我的应用需要使用Retoofit和Otto下载远程数据。大部分工作已经完成,除了奥托的订户永远不会被召唤。

  • 通过改造下载数据,完成
  • 实施改造回调,完成
  • 发布者将结果回拨给otto事件总线,完成
  • 订阅者从otto事件总线获取结果,而不是

问题

  • 奥托的订阅者永远不会被打电话。

代码

MyApplication.java

     public class MyApplication extends Application{
     private static Bus mBus;
     @Override
       public void onCreate() {
       super.onCreate(); 
    }

    public static Bus getEventBus() {
    if(mBus==null) {
        mBus = new Bus(ThreadEnforcer.ANY);
    }
    return mBus;
   }
}

MyFavouriteFragment.java

public class MyFavouriteFragment extends Fragment implements AdapterView.OnItemClickListener {

public MyFavouriteFragment() {
}

public static MyFavouriteFragment newInstance() {
    MyFavouriteFragment fragment = new MyFavouriteFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(MainActivity.TAG, "oncreate register bus");
    MyApplication.getEventBus().register(getActivity());
}


@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
    MyApplication.getEventBus().unregister(getActivity());
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // apply first download before setOnScrollListener
    downloadAds(0);
}


private void downloadAds(final int getAdFromIndex) {
    // download data from server
    RestClient.getApiService().getFavouriteAd(
            getAdFromIndex,
            Constant.numOfAdPerFetch,
            userId,
            true,
            new GetMyFavouriteAd.Callback());
}

// This one never get called !!!!!!!!!!!!!!!!!!!!!!!!!!
@Subscribe
public void onGetMyFavouriteAdEvent(GetMyFavouriteAd.GetMyFavouriteAdEvent event){
    Log.d(MainActivity.TAG, "onGetMyFavouriteAdEvent");
    postDownloadSetup();
}

@Subscribe
public void onRetrofitErrorEvent(RetrofitErrorEvent event){
    Log.d(MainActivity.TAG, "getFavouriteAd failed " + event.getError().toString());
    postDownloadSetup();
}
}

GetMyFavouriteAd.java

 public class GetMyFavouriteAd {

public static final class Callback implements retrofit.Callback<List<GenericAd>> {
    @Override
    public void success(List<GenericAd> genericAdList, Response response) {
        Log.d(MainActivity.TAG, "GetMyFavouriteAd call back");
        MyApplication.getEventBus().post(new GetMyFavouriteAdEvent());
    }

    @Override
    public void failure(RetrofitError error) {
        Log.d(MainActivity.TAG, "GetMyFavouriteAd call back failed");
        MyApplication.getEventBus().post(new RetrofitErrorEvent(error));
    }
}

// Otto Event
public static final class GetMyFavouriteAdEvent {
    List<GenericAd> genericAdList;
    Response response;

    public GetMyFavouriteAdEvent(){
        // this one get called successfully
        Log.d(MainActivity.TAG, "ini GetMyFavouriteAdEvent");
    }

    public List<GenericAd> getGenericAdList() {
        return genericAdList;
    }

    public void setGenericAdList(List<GenericAd> genericAdList) {
        this.genericAdList = genericAdList;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }
}
}

RestClient.java

  public class RestClient {
private static ApiService apiService;

public static ApiService getApiService() {
    if (apiService == null) {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(Constant.baseUri)
                .build();
        apiService = restAdapter.create(ApiService.class);
    }

    return apiService;
}
}

ApiService.java

  public interface ApiService {
    // get favourite ad
@FormUrlEncoded
@POST("/getFavouriteAdByUserId.php")
void getFavouriteAd(
        @Field("getAdFromIndex") int getAdFromIndex,
        @Field("numOfAdPerFetch") int numOfAdPerFetch,
        @Field("userId") String userId,
        // true to return ad.*, false to return adId only
        @Field("getAdDetails") boolean getAdDetails,
        Callback<List<GenericAd>> cb
);
}

1 个答案:

答案 0 :(得分:2)

注册/取消注册片段而不是活动

 MyApplication.getEventBus().register(getActivity());

 MyApplication.getEventBus().register(this);

取消注册

相同