【问题标题】:How to run socket code in another threads?如何在另一个线程中运行套接字代码?
【发布时间】:2018-06-01 12:49:40
【问题描述】:

在我的应用程序中,我有 adapter,在这个 adapter 中我应该调用 socket
我想在 另一个线程 中调用 socket 而不是在 MainThread 中调用。
我编写了 socket 代码,但我不知道如何在 另一个线程 中调用它。

我的套接字代码:

mSocket.on("finish", new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        try {
            Constants.currentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("socketLogs", args[0] + "");
                    try {
                        startTimer();
                        final FinishResponse finishResponse = new Gson().fromJson(args[0].toString(), FinishResponse.class);
                        countDownerLayout.setVisibility(View.GONE);
                        winnerLay.setVisibility(View.VISIBLE);
                        bidCount.setVisibility(View.GONE);
                        offerCount.setVisibility(View.GONE);
                        price.setVisibility(View.GONE);
                        timeView.setVisibility(View.GONE);
                        userPic.setVisibility(View.GONE);
                        winnerLay.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
                        //if (finishResponse.getRes().getWinnerAvatar() != null && !finishResponse.getRes().getWinnerAvatar().equals("")) {
                            Glide.with(context)
                                    .load(Constants.SERVER + finishResponse.getRes().getWinnerAvatar())
                                    .placeholder(R.mipmap.ic_launcher)
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .into(User);
                       /* } else {
                            User.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_launcher_round));
                        }*/
                        edtUserName.setText(finishResponse.getRes().getWinnerName());
                        edtUserName.setTextColor(ContextCompat.getColor(context, R.color.black));
                        txtStartPrice.setText("Sell");
                        txtStartPrice.setBackgroundColor(ContextCompat.getColor(context, R.color.TextColorGreen));
                        txtStartPrice.setTextColor(ContextCompat.getColor(context, R.color.white));
                        try {
                            String[] splitDate = finishResponse.getRes().getEnd().split(" ");
                            String[] dateSpliet = splitDate[0].split("-");
                            TimeUtils timeUtils = new TimeUtils(Integer.parseInt(dateSpliet[0]), Integer.parseInt(dateSpliet[1]), Integer.parseInt(dateSpliet[2]));
                            txtPrice.setText(splitDate[1] + "  " + timeUtils.getIranianDate());
                        } catch (Exception e) {
                        }
                    } catch (JsonSyntaxException e) {
                    }
                }
            });
        } catch (Exception e) {

        }
    }
});

我在 AdaptergetView 方法中编写上述代码。
但我想在 另一个线程 中编写上面的代码,并且不在 MainThread 中运行

我该怎么做?

【问题讨论】:

    标签: java android multithreading sockets


    【解决方案1】:

    只需为此创建新线程

    Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    // do stuff
                }
    };
    new Thread(runnable).start();
    

    我会就您的代码提出一些建议。首先,在getView中调用适配器中的Socket是不好的。您应该在片段或活动中调用它(如果您不使用 MVP 或其他架构)。

    下一个大问题是您将当前活动保存在常量中

    Constants.currentActivity
    

    它会导致内存泄漏,即使你关闭它,你的活动也会继续存在,并且你的应用程序会遇到很多问题。

    【讨论】:

    • 谢谢我的朋友。如果我在片段中调用套接字,我如何在收到套接字响应时更新适配器?我使用 ArrayAdapter 和 ListView 。请帮帮我
    • 你可以做两件事:1)当你从socket接收到数据时,更新Adapter的List并调用adapter.setItems(newArrayList); adapter.notifyDataSetChanged() 和适配器将刷新自己 2) 或只是将新适配器设置为列表。
    • 我该怎么办?你能帮我把代码发给我吗?请
    • 我为呼叫适配器编写了这段代码:AuctionTodayListAdapter adapter = new AuctionTodayListAdapter(getActivity(), R.layout.list_item_auction_large_new, Constants.auction.getToday()); list.setAdapter(adapter);
    • 太棒了,1) 将方法 updateItems(Arraylist items) 添加到您的 AuctionTodayListAdapter,以更新拍卖列表。 2) 在片段/活动中设置套接字,当您在片段/活动中获得套接字响应以调用适配器的 updateItems 方法时,这将更新适配器内的列表。 3) 调用 adapter.notifyDataSetChanged() 这将更新您的项目。
    猜你喜欢
    • 2012-06-22
    • 2020-05-02
    • 1970-01-01
    • 2022-11-11
    • 2022-11-18
    • 2011-09-15
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多