首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >runOnUiThread有时不起作用-为什么?

runOnUiThread有时不起作用-为什么?
EN

Stack Overflow用户
提问于 2019-07-15 09:30:07
回答 1查看 368关注 0票数 1

我使用OkHttpClient从服务器获取数据并在runOnUiThread事件中显示,但是事件并不总是工作的。

基本的逻辑是我点击一个按钮,OkHttpClient从服务器获得一个json,解析它并向我显示json数据。不知道为什么,有时它很好,但在更多的情况下,我需要点击几次才能奏效。我测试并发现json数据总是被成功地获取,所以问题似乎是runOnUiThread事件没有以正确的方式触发?抱歉,我刚认识android

代码语言:javascript
复制
private void getDocDetail() {
    OkHttpClient client = new OkHttpClient.Builder().build();
    RequestBody post = new FormBody.Builder()
                .add("DocTypeID", Integer.toString(DocTypeID))
                .add("DocID", Integer.toString(DocID))
                .build();

    final Request req = new Request.Builder()
                .url("http://127.0.0.1/GetDocData.php"))
                .post(post)
                .build();

    client.newCall(req).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, final Response response) throws IOException {
            runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    final String s = response.body().string();
                    JSONObject jo = new JSONObject(s);
                    AttachCount = jo.optInt("Attach");
                    edtAttach.setText(String.valueOf(AttachCount));
                } catch (JSONException e) { e.printStackTrace(); }
                        catch (IOException e) { e.printStackTrace(); }
                        catch (Exception e) { e.printStackTrace(); }
            });
            }
    });
}

根本没有错误信息。我需要的是,每次我点击按钮,我就能正确地看到AttachCount,而不是敲击10次,只有1次有效。

当它正常工作时,日志非常简单,如下所示:

2019-07-16 10:56:16.553 13332-13332/com.example.lance.platform I/Timeline: 时间线: cmp=com.example.lance.platform/.DocsignActivity时间:5633500意图:意图{ Activity_launch_request (有附加)} 2019-07-16 10:56:16.624 13332-13379/com.example.lance.platform I/ContentCatcher: ViewContentFetcher : ViewContentFetcher 2019-07-16 10:56:17.093 13332-13337/com.example.lance.platform I/zygote64 64:做完整的代码缓存收集,code=481KB,data=346KB 2019-07-16 10:56:17.095 13332-13337/com.example.lance.platform I/zygote64 64:代码缓存收集后,code=470KB,data=294KB 但是,如果我点击而什么都没有发生,日志就变成如下所示: 2019-07-16 10:48:14.443 12870-12870/com.example.lance.platform I/Timeline:时间线: Activity_launch_request时间:5151391意图:意图{Activity_launch_request(有额外的)} 2019-07-16 10:48:14.554 12870-12896/com.example.lance.platform D/ViewContentFactory: initViewContentFetcherClass 2019-07-16 10:48:14.554 12870-12896/com.example.lance.platform I/ContentCatcher: ViewContentFetcher : ViewContentFetcher 2019-07-16 10:48:14.554 12870-12896/com.example.lance.platform D/ViewContentFactory: createInterceptor采取了1ms 2019-07-16 10:48:14.810 12870-12875/com.example.lance.platform I/zygote64 64:编译器分配了7MB编译void android.widget.TextView.(android.content.Context,android.util.AttributeSet,int,int) 2019-07-16 10:48:14.845 12870-12870/com.example.lance.platform W/Looper:慢帧: doFrame晚了318 is 2019-07-16 10:48:15.065 12870-12870/com.example.lance.platform W/System.err: android.os.NetworkOnMainThreadException 2019-07-16 10:48:15.066 12870-12870/com.example.lance.platform W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450) 2019-07-16 10:48:15.066 12870-12870/com.example.lance.platform W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:169) 2019-07-16 10:48:15.066 12870-12870/com.example.lance.platform W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:139) 2019-07-16 10:48:15.066 12870-12870/com.example.lance.platform W/System.err: at okio.Okio$2.read(Okio.java:139) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okio.AsyncTimeout$2.read(AsyncTimeout.java:237) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okio.RealBufferedSource.read(RealBufferedSource.java:46) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:384) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okhttp3.internal.Util.skipAll(Util.java:171) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okhttp3.internal.Util.discard(Util.java:153) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okhttp3.internal.http1.Http1Codec$FixedLengthSource.close(Http1Codec.java:400) 2019-07-16 10:48:15.067 12870-12870/com.example.lance.platform W/System.err: at okio.RealBufferedSource.close(RealBufferedSource.java:455) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at okhttp3.internal.Util.closeQuietly(Util.java:106) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at okhttp3.ResponseBody.string(ResponseBody.java:177) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at com.example.lance.platform.DocsignActivity$1$1.run(DocsignActivity.java:229) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at android.os.Handler.handleCallback(Handler.java:794) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at android.os.Looper.loop(Looper.java:176) 2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6651) 2019-07-16 10:48:15.069 12870-12870/com.example.lance.platform W/System.err: at java.lang.reflect.Method.invoke(本地方法) 2019-07-16 10:48:15.069 12870-12870/com.example.lance.platform W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 2019-07-16 10:48:15.069 12870-12870/com.example.lance.platform W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 2019-07-16 10:48:15.098 12870-12898/com.example.lance.platform D/OpenGLRenderer: endAllActiveAnimators on 0x6fd8b62000 (ListView),句柄为0x6fd8ac0460

EN

回答 1

Stack Overflow用户

发布于 2019-07-16 09:41:11

这个代码有问题。

代码语言:javascript
复制
   final String s = response.body().string();

如日志所示,

代码语言:javascript
复制
2019-07-16 10:48:15.068 12870-12870/com.example.lance.platform W/System.err: at okhttp3.ResponseBody.string(ResponseBody.java:177)

很有可能网络响应未被完全接收,这将调用网络套接字来获取内容,最终将成为UI线程上的网络。

因此,解决方案是创建一个成员变量mString并在onResponse中设置:

代码语言:javascript
复制
public void onResponse(Call call, final Response response) throws IOException 
{
    mString = response.body().string();

在runOnUiThread中,使用这个mString而不是response.body().string()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57036891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档