我在这里看过一些答案,但他们都问了不同的问题。结果我想出了下面的代码。我使用 AsyncTask 解析我的 JSON,我可以在两个地方访问它。doInBackground() 结束;和 onPostExecute();
我在两者中都尝试了以下代码并得到了两个不同的错误:
编码:
try {
ImageView one = (ImageView)findViewById(R.id.one);
String getImage = bookDet.get(0).image;
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(getImage).getContent());
one.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BookDet 是保存数据的 ArrayList,它已被转换为字符串并放在那里。
我尝试 doInBackground() 时的错误:
E/AndroidRuntime(14007): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(14007): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(14007): at android.os.AsyncTask$3.done(AsyncTask.java:299)
E/AndroidRuntime(14007): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
E/AndroidRuntime(14007): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
E/AndroidRuntime(14007): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
E/AndroidRuntime(14007): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
E/AndroidRuntime(14007): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
E/AndroidRuntime(14007): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
E/AndroidRuntime(14007): at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime(14007): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
这只是其中的一部分,但最后一行似乎是关键。只有一个 AsyncTask 所以是的。
当我在 onPostExecute 中执行此操作时出错:
E/AndroidRuntime(16801): FATAL EXCEPTION: main
E/AndroidRuntime(16801): android.os.NetworkOnMainThreadException
E/AndroidRuntime(16801): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
E/AndroidRuntime(16801): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
E/AndroidRuntime(16801): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/AndroidRuntime(16801): at java.net.InetAddress.getAllByName(InetAddress.java:214)
E/AndroidRuntime(16801): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
E/AndroidRuntime(16801): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
E/AndroidRuntime(16801): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
E/AndroidRuntime(16801): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
E/AndroidRuntime(16801): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
E/AndroidRuntime(16801): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
E/AndroidRuntime(16801): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
E/AndroidRuntime(16801): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
E/AndroidRuntime(16801): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
E/AndroidRuntime(16801): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
E/AndroidRuntime(16801): at java.net.URLConnection.getContent(URLConnection.java:190)
E/AndroidRuntime(16801): at java.net.URL.getContent(URL.java:447)
所以我不能在主线程中做到这一点......?
目前我能做的最好的事情就是有一个 TextView 将那个 url 拉到一个字符串中,并且显示得很好,但这显然是没用的。
有任何想法吗?如何从一组异步完成的解析数据中显示图像?
感谢任何人的帮助。谢谢。