- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试访问服务器以便接收 JSON 字符串。但显然在 Ice Cream Sandwich 中你不能在主线程中进行网络操作,但是 AsyncTask 类让我感到困惑并且无法正常工作。这是我到目前为止所拥有的:
//up in main code
Void blah = null;
URI uri = "kubie.dyndns-home.com/R2Bar2/ingredients.php";
new DownloadFilesTask().execute(uri , blah, blah);
private class DownloadFilesTask extends AsyncTask<URI, Void, Void> {
protected Void doInBackground(URI... uri) {
HttpClient client = new DefaultHttpClient();
String json = "";
int duration = Toast.LENGTH_SHORT;
try {
HttpResponse response = null;
BufferedReader rd = null;
String line = "";
HttpGet request = new HttpGet(uri);
} catch (URISyntaxException e1) {
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
它不喜欢我的 HttpGet request = new HttpGet(uri)
它说要将 uri 更改为 URI,但它已经是了!我已尝试将所有参数更改为 Void,但我的应用程序只是强制关闭。
有人知道怎么做吗?
最佳答案
But apparently in Ice Cream Sandwich you can't do network operations in the main thread
你可以,但它会在 LogCat 中给你一个警告。而且您不应该在主应用程序线程上执行网络操作,因为它会卡住您的 UI,并可能给您一个“应用程序无响应”(ANR) 对话框。
but the AsyncTask class is confusing me and not working
在您当前的代码中,您实际上并未执行 HttpGet
请求。
It's not liking my HttpGet request = new HttpGet(uri) It says to change uri to URI, but it already is!
不,不是。
doInBackground()
声明中 URI
之后的 ...
意味着此方法接受可变数量的参数。您的 uri
参数实际上是一个 URI[]
。如果您只使用一个 URI
调用 execute()
,您将通过 uri[0]
获得该 URI
>,不是 uri
。
关于android - networkOnMainThread 异常 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9729825/
当我尝试实现以下代码时出现 NetworkOnMainThreadException: public class HandlingXMLStuff extends ListActivity{ stat
我正在尝试访问服务器以便接收 JSON 字符串。但显然在 Ice Cream Sandwich 中你不能在主线程中进行网络操作,但是 AsyncTask 类让我感到困惑并且无法正常工作。这是我到目前为
我遇到网络速度非常慢的问题。出于测试目的,我尝试使用 Android 4.0.4 完全切断 asynctask,当然我需要避免在主线程上进行网络调用,否则会出现 networkNotOnMainThr
我创建了一个应用程序,它使用 xml pull 解析器从 url 检索数据。它在模拟器上工作正常,但是当我在选项卡中测试它时,它显示 networkonmainthread 异常。我的代码是: pac
我在运行 https 时收到有关 Lollipop+ 上 NetworkOnMainThread 异常的报告api 调用使用 Retrofit和 RxAndroid . 我已经隔离了代码并创建了以下仍
我的应用程序中的一件事有问题。当我在调试器中启动应用程序时,它会抛出一个错误NetworkOnMainThreadException。更具体地说,当在线程(而不是主线程)中连接到 Web 服务器时会引
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (65 个回答) 已关闭 4 年前。 我收到"android.
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个答案) 关闭 9 年前。 我在我的 andr
我尝试过使用 Scheduler.newThread() 和 Scheduler.io() 但还是不行这是我的代码 Observable loginObservable = Observable.cr
我正在制作一个 Android 应用程序来从用户获取数据并将其存储在 mongodb 数据库中 mongodb 在 localhost:27017 上运行,我可以在 genymotion 模拟器上使用
我有以下 rxJava 链: override fun combineLocationToPlace(req: Flowable): Flowable { var combinedF
这个问题已经有答案了: NetworkOnMainThread (3 个回答) 已关闭 7 年前。 我正在使用 XmlPullParser 从服务器解析文件,当然我是在另一个线程中执行的。我正在使用
我目前正在 Android 上试验 RxJava,在尝试将 AmbWith 运算符与两个 Observable 一起使用时遇到了问题。 正如我在此处的 amb 定义中所理解的 http://react
下面的代码始终在 Android 上抛出 NetworkOnMainThread,调用尝试是获取位置并使用它通过 RetroFit 2 API 获取列表,这在其他情况下工作正常,ReactiveLoc
我正在尝试在 Android 应用程序中使用 Google map 。 MainActivity 包含EditText、ImageButton 和MapView。当用户提供一些查询并点击 ImageB
您好,我正在研究 facebook api。我已经阅读了 facebook 文档并按照其中的说明进行了编码。下面的代码在 android 2.1-3.2 中运行良好,但是在 android 4.0.4
我是一名优秀的程序员,十分优秀!