android - 与奥托一起改造 - 订阅未调用

标签 android retrofit otto

我在这个问题上花了很多时间,但仍然找不到正确的方法。 希望有人能帮忙。

情况

我的应用程序需要使用 Retrofit 和 Otto 下载远程数据。大部分工作已经完成,只是 Otto 的订阅者从未接到电话。

  • 通过改造下载数据,完成
  • 实现改造回调,完成
  • 发布商将回调结果发布到 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
);
}

最佳答案

注册/取消注册 Fragment 而不是 Activity

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

 MyApplication.getEventBus().register(this);

注销也一样

关于android - 与奥托一起改造 - 订阅未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524457/

相关文章:

Android - gridview滚动问题(滚动非常慢)

java - 使用 Retrofit/Rxjava 进行数据绑定(bind)/实时数据处理配置更改

java - 如何使用 RxJava 取消 Retrofit 中的单个网络请求?

java - 使用 Otto 进行线程间通信 : Can it cause any problems?

java - 如何从 Android 中的普通 java 类调用 Activity 类

android - Android 中的 OpenCv : keypoint detection in images from file

android - 注入(inject) Otto 事件总线而不是使用静态单例的优势

android - 事件总线 : How to keep track?

android - 如何以原始方向将位图保存为 Jpg 图像?

java - 无法使用 Retrofit 和 PHP 作为后端从 android 中的服务器接收任何响应