尝试实现 okHttp 调用和回调方案。完成后没有 onPostExecute 修改 UI

Trying to implement the okHttp Call & Callback scenario. Upon completion there is no onPostExecute to modify the UI

以前我只是将 response.body().string() 传递给 asyncTaskOnPostExecute() 方法来更新 UITextViewsokHttp好像没有这个选项。等效项在哪里或如何实现?

//Call to server to echo MySQL query
     private void usersInZone() {
                if (!isNetworkAvailable()) return;
                rotate(true);
                OkHttpClient client = new OkHttpClient();
                RequestBody formBody = new FormBody.Builder()
                        .add("userId", String.valueOf(userId))
                        .add("channel", String.valueOf(subport + port))
                        .build();
                Request request = new Request.Builder()
                        .url(SITE_URL + "inZone.php")
                        .post(formBody)
                        .build();

                Call users = client.newCall(request);
                users.enqueue(caller);
            }

//okHttp Callback to parse echo response
    caller = new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (RS != null) RS.makePoor(true);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    LOG.l(String.valueOf(call));
                    if (response.isSuccessful()) {
                        try {
                            JSONObject data = new JSONObject(response.body().string());
                            updateFields(getString(R.string.inzone) + data.getInt("total") + "]", String.valueOf(data.getInt("zone")));
                        } catch (JSONException e) {
                            LOG.l("JSONException" + e);
                        }
                    } else if (RS != null) RS.makePoor(true);

                }
            };
//UI textViews i'm needing to update with the echoed results
    private void updateFields(String total, String channelTotal){
            subtitle.setText(total);
            tvusercount.setText(channelTotal);
        }

Where is the equivalent or how to achieve this?

OkHttp 的 onResponse() 未在主线程上调用。如果您想在 onResponse 中更新 UI,您必须将此任务委托给 UI 线程。使用 Handler 并发布 Runnable 是一种方法。

回调没有在主线程上调用。如果您需要更新 UI,一个简单的方法是创建一个 class 实现 Callback 和 post 项目到 UI 之类的所以:

  public abstract class UIMainCallback implements Callback {
    private static final Handler mUIHandler = new Handler(Looper.getMainLooper());

    abstract void failure(Request request, IOException e);

    abstract void response(Response response);

    @Override
    public void onFailure(final Request request, final IOException e) {
        mUIHandler.post(new Runnable() {
            @Override
            public void run() {
                failure(request, e);
            }
        });
    }

    @Override
    public void onResponse(final Response response) throws IOException {
        mUIHandler.post(new Runnable() {
            @Override
            public void run() {
                response(response);
            }
        });
    }
}

然后您的 Caller 可以只实现 UIMainCallback 而不是接口。