Android JSON解析并插入sqlite
只是为了理解。
目前我有一个异步任务,其工作原理如下:
String doInBackground(Void... params)
//Downloads a JSON and returns it to the onPostExecute().
void onPostExecute(String response)
//Parses the JSON which has an array of stuff and insert into sqlite.
Call changeCursosr and notifyDataSetChanged.
这是处理列表视图的插入和刷新的正确方法吗?目前,每当它执行后,我都会遇到轻微的挂起。我应该解析&将数据插入到 doInBackground 中?
Just for understanding.
Currently I have an asynctask that works like this:
String doInBackground(Void... params)
//Downloads a JSON and returns it to the onPostExecute().
void onPostExecute(String response)
//Parses the JSON which has an array of stuff and insert into sqlite.
Call changeCursosr and notifyDataSetChanged.
Is this the right way to handle the insertion and refreshing of the listview? Currently I'm experiencing a slight hang whenever it hits the post execute. should I be parsing & inserting the data in the doInBackground instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
onPreExecute
和onPostExecute
不适合任何流程密集型的事情。它们通常保留用于执行诸如启动和停止ProgressDialog
之类的操作。除了处理来自这些资源的数据之外,您还应该在 doInBackground 内进行网络调用和数据库调用。
onPreExecute
andonPostExecute
are not suited for any process-intensive things. They are generally reserved for doing things such as starting and stopping aProgressDialog
.You should be making network calls as well as database calls inside
doInBackground
in addition to processing the data from those resources.来自 AsyncTask docs:
onPostExecute(Result),在 UI 线程上调用后台计算完成后。后台计算的结果作为参数传递到此步骤。
由于它是在UI线程中调用的,所以它本质上是一个阻塞过程。所有繁重的工作都应该在 doOnBackground 中完成。
From the AsyncTask docs:
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
Since it is invoked in the UI thread, it is essentially a blocking process. All the heavy lifting should be done in doOnBackground.