作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 android studio 项目中,我使用 okhttp 发送 post 请求。
public void sentRequest(.... ....) {
RequestBody formBody = new FormBody.Builder().add("number", number).build();
Request request = new Request.Builder().url(serverBase + API_numberAvailabilityCheck).post(formBody).build();
call = client.newCall(request);
new Thread(new Runnable() {
@Override
public void run() {
final HashMap<String, Object> requestResult = new HashMap<>();
try {
final Response response = call.execute();
}
...........
...........
}
}).run();
}
但它在我调用 final Response response = call.execute();
的地方抛出 NetworkOnMainThreadException
异常,尽管 new Thread
。为什么它会抛出异常?以及如何解决这个问题?
最佳答案
错误是
thread.run()
而不是 thread.start()
。
调用run()
方法,代码在同一个线程中运行,而不启动新线程。
关于java - okHttp NetworkOnMainThreadException 尽管有新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57337659/
嘿。本周的一个教程,其中一个问题要求通过使用其他函数 formatLine 和 formatList 创建一个函数 formatLines,以格式化行列表。 我的代码是这样的; type Line =
我是一名优秀的程序员,十分优秀!