Search code examples
androidevent-bus

How to use EventBus library in android


I want develop android appliacation for one site, in this application i want use oKHttp v3, and EventBus v3. eventbus library source link : LINK.
I write below codes, but when running application, show me FC error!
okHttp_Page(main activity) codes:

public class okHTTP_Page extends AppCompatActivity {

    private RecyclerView recycler;
    private okHTTP_adapter adaper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ok_http__page);
        EventBus.getDefault().register(this);

        recycler = (RecyclerView) findViewById(R.id.okHTTP_recycler);
        recycler.setHasFixedSize(true);
        recycler.setLayoutManager(new LinearLayoutManager(this));

        okHTTP_info info = new okHTTP_info();
        info.getOkHTTP_info(this);

        adaper = new okHTTP_adapter(this);
        recycler.setAdapter(adaper);

    }
}

okHttp_info(AsyncTask class) codes:

public class okHTTP_info {
    private Context mContext;

    public void getOkHTTP_info(Context context) {
        mContext = context;
        new getInfo().execute(serverIP.getIP());
    }

    private class getInfo extends AsyncTask<String, Void, String> {
        EventBus bus = EventBus.getDefault();
        private String ou_response;
        private List<okHTTP_dataProvider> infoModels;

        @Override
        protected void onPreExecute() {
            CustomProcessDialog.createAndShow(mContext);
            infoModels = new ArrayList<>();
        }

        @Override
        protected String doInBackground(String... params) {
            OkHttpClient client = new OkHttpClient();

            RequestBody requestBody = new MultipartBody.Builder()
                    .addFormDataPart("test", "2")
                    .addFormDataPart("posts", params[0])
                    .build();

            Request request = new Request.Builder()
                    .url(serverIP.getIP())
                    .post(requestBody)
                    .build();

            Response response;
            try {
                response = client.newCall(request).execute();
                ou_response = response.body().string();
                response.body().close();
                if (ou_response != null) {
                    try {
                        JSONObject postObj = new JSONObject(ou_response);
                        JSONArray postsArray = postObj.getJSONArray("posts");
                        infoModels = new ArrayList<>();

                        for (int i = 0; i < postsArray.length(); i++) {
                            JSONObject postObject = postsArray.getJSONObject(i);
                            int id = postObject.getInt("id");
                            String title = postObject.getString("title");
                            Log.d("Data", "Post id: " + id);
                            Log.d("Data", "Post title: " + title);

                            //Use the title and id as per your requirement
                            infoModels.add(new okHTTP_dataProvider(
                                    postObject.getString("title"),
                                    postObject.getInt("id")));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ou_response;
        }

        @Override
        protected void onPostExecute(String result) {
            CustomProcessDialog.dissmis();
            if (result != null) {
                bus.post(infoModels);
            }
        }
    }
}

LogCat errors:

04-16 16:04:34.780 20445-20445/com.tellfa.okhttpproject E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.okhttpproject/com.tellfa.okhttpproject.Activities.okHTTP_Page}: org.greenrobot.eventbus.EventBusException: Subscriber class com.tellfa.okhttpproject.Activities.okHTTP_Page and its super classes have no public methods with the @Subscribe annotation at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5069) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.tellfa.okhttpproject.Activities.okHTTP_Page and its super classes have no public methods with the @Subscribe annotation at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67) at org.greenrobot.eventbus.EventBus.register(EventBus.java:136) at com.tellfa.okhttpproject.Activities.okHTTP_Page.onCreate(okHTTP_Page.java:23) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)  at android.app.ActivityThread.access$600(ActivityThread.java:141)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:5069)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)  at dalvik.system.NativeStart.main(Native Method)

 


Solution

  • you just need to add onEvent method in your activity(okHTTP_Page) with @Subscribe annotation

    @Subscribe
    public void onEvent(List<okHTTP_dataProvider> infoModels) {
    
    };