从网络服务读取数据的推荐方式/顺序,解析该数据并将其插入到 SQLite 数据库中 [英] Recommended way / order to read data from a webservice, parse that data and insert it in a SQLite db

查看:20
本文介绍了从网络服务读取数据的推荐方式/顺序,解析该数据并将其插入到 SQLite 数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始提到我对 Android 完全陌生,我刚刚读完一本快速的介绍性书籍,现在我必须实现我的第一个应用程序.这个应用程序将用于接受订单.在我要存储在本地数据库中的所有数据中,有两个表是最重要的:Customers 和 Articles,后者是所有表中最大的(大约 20000 条记录)我的应用程序中的主要进程之一,当用户按下启动设备上日常操作的按钮时,获取我的应用程序离线工作所需的所有数据.

I'm one to start mentioning that I'm totally new to Android, I've just finished reading a quick, introductory book and now I have to implement my very first app. This app is going to be use to take orders. Among all the data I'm going to store in a local db, two tables are the most important: Customers and Articles, being the latter the largest of all the tables (aprox 20000 records) One of the main process in my app, fetches all the data that my app need to work off-line when the user press a button that starts the daily operations on the device.

嗯,这个过程包括以下步骤:

Well, the process consists of the following steps :

一个.读取 Restful 服务以检索客户数据
湾解析对 Json 对象的响应
C.将这些客户插入到客户表中
d.读取 Restful 服务以检索文章数据
e.解析对 Json 对象的响应
F.将这些文章插入文章表

a. Read a restful service to retrieve the Customers Data
b. Parse the response to Json Objects
c. Insert those customers to the Customers Table
d. Read a restful service to retrieve the Articles Data
e. Parse the response to Json Objects
f. Insert those articles to the Articles Table

这是我计划做的:

  1. 编写一个帮助类来处理所有的 HTTP GET 请求.然后每当我需要下载所有客户和文章数据时调用这个类
  2. 由于所有这些过程可能需要很多时间,所以我需要在后台进行.因此,根据一些建议,我将把所有这些代码放在一个绑定服务中.
  3. 虽然所有这些漫长的处理都在后台进行,但我必须显示某种指标(ProgressDialog)这就是我选择使用绑定服务的原因

虽然我认为我已经大致了解如何单独做这些事情,但我认为将所有事情放在一起是完全不同的故事.

Though I think I've got the general idea of how to do most of these thing separately, I think that putting the all together is quite a different story.

  1. 您认为我执行所描述过程的所有 6 个步骤的顺序是否正确/有效?如果你必须做出一些改变,你会改变什么?
  2. 如果启动服务的活动被明确取消或被其他活动隐藏,则服务必须有办法让用户知道该过程已完成.我该如何实施?
  3. 是否可以/建议在服务中写入 SQLite 数据库?是否与我在活动中这样做相同?
  4. 在J2ME中我做过类似的事情,当我把上面提到的6个步骤放在一起时,它们都是按顺序执行的,即一个接一个.安卓也一样吗?

我希望我没有问太多问题.我只是把它们放在一起,因为它们都是相关的.

I hope I'm not asking too many questions. I just put them together because they are all related.

我决定来找你,因为你是专家,我真的需要有人来指导我走向正确的方向.

I decided to come to you because you're the experts and I really need someone to put me in the right direction.

非常感谢.

附言请不要关闭此问题,如果您认为我需要更改某些内容,请告诉我,我会这样做.

P.S. Please do not close this question, if your think I need to change something just let me know and I'll do it.

推荐答案

我决定来找你,因为你是专家

I decided to come to you because you're the experts

首先我不是专家而且我不了解你可以找到比我更多的专家,但这是我的意见 希望能给你一些帮助.

first i am not expert and also i am not knowledgeable you can find more expert people than me but this is my opinion hope to give you some help.

首先忘记使用 AsyncTask 下载,因为它必须用于与您不同的短后台作业,我认为您要下载的文件量非常大.(我认为是)

first of all forget to use AsyncTask to download because it must be used for short background jobs not like yours i think the amount of file you want to download is pretty large.(i think so)

检查谷歌的 downloadmanager 看看它是如何工作的,它可能对你有帮助.

check downloadmanager of google to see how it works it may help you.

http://developer.android.com/reference/android/app/DownloadManager.html

http://blog.vogella.com/2011/06/14/android-downloadmanager-example/

如果你想使用 service 使用 unbound service 因为你不希望当用户关闭应用程序时服务被 android 或用户破坏,你是吗?我认为您想以任何方式获取数据.

if you want to use service use unbound service because you do not want the service to be destroyed by android or user when the user close the apps do you? i think you want to get your data any way.

为了提高效率,我推荐以下步骤:

in order to be efficient i recommend these steps:

一个.读取 Restful 服务以检索客户数据

a. Read a restful service to retrieve the Customers Data

B.当您获得客户数据时:

b. when you get Customer data do :

  • 创建另一个服务或线程来读取一个安静的服务来检索文章数据

  • create another service or thread to Read a restful service to retrieve the Articles Data

在旧服务或线程上解析对 Json 对象的响应

Parse the response to Json Objects on your old service or thread

现在您有 2 个并发运行的服务或线程,因此请在每个服务或线程上执行明显的insert parse 等步骤.

now you have 2 services or threads that run concurrently so follow the steps that obvious insert parse and so, on each service or thread.

为什么我不将 a 和 d 结合起来?因为我认为用户不喜欢在下载进度条后面等待太多时间.

why do not i combine a and d? because i think user do not like to wait much time behind download progress bar.

为了将您的数据插入数据库使用 transaction,我建议您使用:

in order to insert your data to database use transaction and i recommend you use:

http://greendao-orm.com/

它比其他数据库更高效ORM,并且您可以从数据库实现中解脱出来.

it is more efficient ORM than others for database and you get free from db implementation.

如果启动服务的活动被明确取消或被另一个活动隐藏,服务必须有办法让用户知道该过程已经完成.我该如何实施?

If the activity that started the service is explicitly cancelled or is hidden by another activity, the service has to have a way to let the user know that the process has finished. How could I implement that?

使用通知:

http://developer.android.com/training/notify-user/build-notification.html

http://www.tutorialspoint.com/android/android_notifications.htm

http://www.vogella.com/tutorials/AndroidNotifications/article.html

虽然所有这些漫长的处理都在后台进行,但我必须显示某种指标(ProgressDialog)这就是我选择使用绑定服务的原因`

While all this long processing is taking place in the background I'll have to show some sort of indicator (a ProgressDialog) This is the reason I opted for using a Bound Service`

如何从未绑定的服务更新 UI?`

how can I update the UI from an Unbound Service?`

使用 LocalBroadCastManager,或者通常使用 BroadCastReciever

Use a LocalBroadCastManager, or in general BroadCastReciever

来自服务的 Android 更新活动 UI

在J2ME中我做过类似的事情,当我把上面提到的6个步骤放在一起时,它们都是按顺序执行的,即一个接一个.安卓也一样吗?

In J2ME I've done something similar, and when I put something like the 6 steps I mentioned above all of them are executed sequentially, that is , one after the other. Is it the same in Android?

这取决于你的步骤,如果你按照我的想法运行,你会concurrent,如果你运行你的想法,你将sequentially运行.

this is depends on your steps, if you follow my idea you run concurrently and if you run your idea you will run sequentially.

祝你好运.

这篇关于从网络服务读取数据的推荐方式/顺序,解析该数据并将其插入到 SQLite 数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆