-1

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)

 

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
FeedBack
  • 87
  • 9
  • 1
    Looking at the logcat, *ts super classes have no public methods with the @Subscribe annotation at* You obviously did not read the documentation for the EventBus, you need to add the annotation to your activity. – t0mm13b Apr 16 '16 at 08:45

3 Answers3

3

You need to add one mehtod which is annotated with @Subscribe. That method also needs to take one parameter which is the type you are posting. So add this to your AppCompatActivity:

@Subscribe
public void subscribeMethod(List infoModels) {
    // Do stuff...
}

Also I would like to recommend not just post the List object. Create an event class for this which wraps the List so your code looks more readable if you need to post other Lists too.

public static class InfoModelsEvent {
    private List infoModels = new ArrayList<>();
    public InfoModelsEvent(List infomodels) {
        this.infoModels = infoModels;
    }
    // Getter here...
}

And then the method in your AppCompatActivity would look like:

@Subscribe
public void subscribeMethod(InfoModelsEvent infoModelsEvent) {
    // Do stuff...
}

And in your AsyncTask you do the posting like this:

bus.post(new InfoModelsEvent(infoModels));

This prevents you from getting in trouble when mistakenly posting other List objects. :)

vilpe89
  • 4,656
  • 1
  • 29
  • 36
  • Thank you man, but i am amateur. can you help me with my above code? please . i really need this tutorial – FeedBack Apr 16 '16 at 09:13
  • Hey, I just explained what your problem is in your code. Just add that first method that I told you to the AppCompatActivity. – vilpe89 Apr 16 '16 at 09:18
1

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

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

};
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

you need add @Subscribe annotation at onEventMainThread() method, like this

@Subscribe
public void onEventMainThread(){

}
段延锐
  • 41
  • 2