你的表达方式似乎是最佳的。也许您应该设置一个与时间或日期相关的标志或警报......以防应用程序在没有互联网的情况下启动太多次。
>>对于移动应用程序的更新,您应该考虑在服务器和应用程序上拥有相同数据的优先级/紧迫性。
>对于更好的方法,您可以选择更适合您要求的方法。
获取数据在一个线程中并在另一个线程中渲染它,
1.编写自定义Asynctasks
:
http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/os/AsyncTask.html
AsyncTask Android 示例 https://stackoverflow.com/questions/9671546/asynctask-android-example
OR
2.使用类似 AsyncHttpClient 的东西:http://loopj.com/android-async-http/ http://loopj.com/android-async-http/
你在哪里得到onSuccess
and onFailure
处理响应的方法。
选项2.如果您只想获取数据而不执行任何其他操作,然后对其进行处理或保存,则更好。同样,您需要首先解析响应。
解析您的数据:
正如你的回应是JSON
格式,你可能最好使用Gson
映射数据并使用自定义模型类。例如。:
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/ https://code.google.com/p/google-gson/
对于命名差异(根据Web服务中的变量),可以使用注释,例如@SerializedName
.
使用 for every 循环来验证/浏览/访问将填充在/作为模型类的对象/字段中的数据:
检查这些是否有疑问:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
Java 的“foreach”循环是如何工作的? https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work
现在关于保存您的数据:
>>这在很大程度上取决于服务器的数据是什么以及您想要存储多少数据。
在 Android 存储选项中:
http://developer.android.com/guide/topics/data/data-storage.html http://developer.android.com/guide/topics/data/data-storage.html
a.有共同的偏好:
这些非常适合保存/存储尺寸相对较小且可能被频繁覆盖和获取的数据。Eg.用户名、当前用户的详细信息、密码
http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/reference/android/content/SharedPreferences.html
如何在 Android 中使用 SharedPreferences 来存储、获取和编辑值 https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values
b.维护数据库对于应用程序中所需的更大块很有好处。
您可以根据需要存储、更新或覆盖数据。可以有多个表,或者可以在各个字段中存储更多数据。
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html http://www.vogella.com/tutorials/AndroidSQLite/article.html
当你使用Gson
,您还可以选择填充模型类的对象,然后将该响应存储在String
(也许在SharedPreferences
,取决于长度/尺寸)使用gsonToJson
方法。我用过这个,虽然厚颜无耻,但很有效。
您还需要考虑其他与 UI 和内存优化相关的内容,ListViews
或布局等,具体取决于您的应用程序及其控制流程。