从服务器加载数据的最佳做法(Android App) [英] Best Practice for loading data from server (Android App)

查看:172
本文介绍了从服务器加载数据的最佳做法(Android App)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Android应用,该应用应该:

显示一些数据,这些数据是从Internet上的服务器加载的。

I'm building an Android app, which should:
show some data, loaded from a server in the Internet.

此刻,我在应用程序中使用了本地 SQliteDB 存储数据,应将其显示。我之所以使用它,是因为即使暂时没有可用的互联网连接,我也希望能够显示数据。

At the moment I have a local SQliteDB used in my app where the data is stored, which should be displayed. I use this, because I want to be able to show the data, even if there is temporarily no internet connection available.

下一步,我将通过互联网服务器将数据插入本地 SQliteDB 中。我考虑过这样做:

Next step I will work on inserting data in the local SQliteDB from a internet server. I thought about doing it this way:

当应用启动时,请检查互联网是否可用。如果是,请连接到Web服务(包括用户名和密码)。 Web服务应通过json对象将必需的数据传递到应用程序,然后我将更新本地SQlite数据库。

我的问题:

这是个好主意吗?

还有其他更好的方法吗?

Zend网站也可以查看(和编辑)数据。

The data can be viewed (and edited) by a Zend Website, too.

预先感谢。

最诚挚的问候
Daniel

Best regards Daniel

推荐答案

您的放置方式似乎是最佳的。也许您应该设置与时间或日期相关的标志或警报。以防应用程序启动太多次而没有互联网。

The way you put it seems optimal. Maybe you should set a flag or alert which is time or date related..in case the app starts too many times without internet.


>> 对于移动应用程序的更新,您应该考虑拥有相同数据的优先级/紧迫性在服务器和您的应用上。

>> For updates to your mobile app, you should consider the priority/urgency of having the same data on the server and your app.

> 为了获得更好的方法,您可以选择更适合自己需求的方法。

> For the better ways to do it, you can opt the way which suits your requirement better.

要在一个线程中获取数据并在另一个线程中进行渲染,

To fetch the data in one thread and render it in another,

1。。编写自定义 Asynctasks

http://developer.android.com/reference/android/os/AsyncTask.html

AsyncTask Android示例

OR

2。。使用类似AsyncHttpClient的名称: http://loopj.com/android-async-http/

,您将在其中获得 onSuccess onFailure 处理响应的方法。

2. Use something like AsyncHttpClient: http://loopj.com/android-async-http/
where you get onSuccess and onFailure methods to work with the response.

选项 2。更好。希望不做任何其他事情就获取数据,然后对其进行处理或保存。同样,您需要先解析响应。

The option 2. is better if you just want to fetch data without doing anything else, and work on it, or save it. For the same, you need to parse the response first.

解析数据:

因为您的响应是 JSON 格式,最好使用 Gson 映射数据并使用自定义模型类。例如:

To parse your data:
As your response is JSON format, you may be better off using Gson to map data and with custom model classes. Eg.:

    Gson gson = new Gson();
    ModelClass modelClass= new ModelClass();
    modelClass= gson.fromJson(responseContent,ModelClass.class); 
//where responseContent is your jsonString
    Log.i("Web service response", ""+modelClass.toString());

更多信息:> https://code.google.com/p/google-gson/

用于命名差异(根据网络服务中的变量),可以使用
@SerializedName 之类的注释。

For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName.

对每个循环使用a来验证/浏览/访问将在模型类的对象/字段中填充的数据:

检查这些是否有疑问:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

Java for each循环如何工作?

Use a for each loop to verify/browse/access the data that would be populated in/as objects/fields of your model class:
Check these for doubts:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?

现在要保存数据:


>> 服务器中的数据是,您要存储多少数据。

>> It depends a lot on what the data from server is and how much data do you want to store.

在Android存储选项中:

http://developer.android.com/guide/topics/data/data-storage.html

In Android Storage Options:
http://developer.android.com/guide/topics/data/data-storage.html

a。。有共享的首选项:

这些参数对于保存/存储较小的数据并且可以被覆盖和获取的数据很有用。经常。 例如用户名,当前用户的详细信息,密码

http://developer.android.com/reference/android/content/SharedPreferences.html

如何在Android中使用SharedPreferences来存储,获取和编辑值

a. There's Shared Preferences:
These are good for saving/storing data which would be relatively small in size and could be overwritten and fetched frequently. Eg. username, current user's details, password
http://developer.android.com/reference/android/content/SharedPreferences.html
How to use SharedPreferences in Android to store, fetch and edit values

b。。维护数据库很适合应用程序中所需的更大块。

您可以存储,更新或覆盖数据。根据您的需要。可以有多个表,也可以在各个字段中存储更多数据。

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

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

b. Maintaining a database is good for the larger chunk needed in your app.
You can store, update or over-write the data according to your need. There can be multiple tables or more data could be stored in various fields.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html

使用 Gson 时,还可以选择填充模型类的对象,然后将该响应存储在 String (也许在 SharedPreferences 中,具体取决于长度/大小),使用 gsonToJson 方法。我已经用过了,厚脸皮但是有效。

While you use Gson, you also have the option of populating the objects of model class and then storing that response in a String(maybe in SharedPreferences, depending on length/size) using gsonToJson method. I have used that, cheeky but effective.

您还需要考虑其他方面,涉及UI和内存优化, ListViews 或布局等,具体取决于您应用及其控制流程。

You need to consider other stuff too, pertaining to UI and memory optimization, ListViews or layouts etc depending on your app and its control flow.

这篇关于从服务器加载数据的最佳做法(Android App)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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