如何在android

时间:2016-04-16 08:40:27

标签: android event-bus

我想为一个网站开发Android appliacation,在这个应用程序中我想使用oKHttp v3EventBus v3。 eventbus库源链接:LINK
我写下面的代码,但在运行应用程序时,显示FC错误!
okHttp_Page(主要活动)代码:

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类)代码:

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错误:

  

04-16 16:04:34.780 20445-20445 / com.tellfa.okhttpproject   E / AndroidRuntime:FATAL EXCEPTION:main java.lang.RuntimeException:   无法开始活动   ComponentInfo {com.tellfa.okhttpproject / com.tellfa.okhttpproject.Activities.okHTTP_Page}:   org.greenrobot.eventbus.EventBusException:订阅者类   com.tellfa.okhttpproject.Activities.okHTTP_Page及其超类   在@Subscribe注释中没有公共方法   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)   在android.app.ActivityThread.access $ 600(ActivityThread.java:141)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   在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)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)at   dalvik.system.NativeStart.main(Native方法)引起:   org.greenrobot.eventbus.EventBusException:订阅者类   com.tellfa.okhttpproject.Activities.okHTTP_Page及其超类   在@Subscribe注释中没有公共方法   org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)   在org.greenrobot.eventbus.EventBus.register(EventBus.java:136)at   com.tellfa.okhttpproject.Activities.okHTTP_Page.onCreate(okHTTP_Page.java:23)   在android.app.Activity.performCreate(Activity.java:5104)at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254)   在android.app.ActivityThread.access $ 600(ActivityThread.java:141)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   在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)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)at   dalvik.system.NativeStart.main(原生方法)

3 个答案:

答案 0 :(得分:3)

您需要添加一个用@Subscribe注释的mehtod。该方法还需要采用一个参数,即您要发布的类型。请将此添加到您的AppCompatActivity

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

另外,我建议不要只发布List对象。为此创建一个包装List的事件类,以便在需要发布其他列表时,您的代码看起来更具可读性。

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

然后AppCompatActivity中的方法如下:

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

在您的AsyncTask中,您可以像这样发布:

bus.post(new InfoModelsEvent(infoModels));

这可以防止您在错误地发布其他List对象时遇到麻烦。 :)

答案 1 :(得分:1)

您只需要在活动中添加List方法(okHTTP_Page)onEvent注释

@Subscribe

答案 2 :(得分:0)

您需要在 onEventMainThread()方法中添加 @Subscribe 注释,就像这样

@Subscribe
public void onEventMainThread(){

}