我正在开发一个需要连接到远程服务器的android应用程序。 很多类(实际上是活动)需要连接到同一台服务器,但通过不同的RESTful Web服务。 而不是在每个类中都有硬编码的服务器地址(或http url),是否有任何可以指示此类信息的轻属性文件(如果可能,xml)的选项(所以如果url / ip发生变化,我只需要修改这个文件,而不是整个源代码),我该如何管理这个文件? 仅供参考:我使用httpclient( http://loopj.com/android-async-http/ )。
I'm developing an android application that needs to connect to a remote server. A lot of classes (actually activity) need to connect to the same server, but through different RESTful web services. Instead of have the address of the server (or http url) hardcoded in each of those classes, is there any option of having a light properties file (xml if possible) that can indicate such information (so if the url/ip change, I just have to modify this file and not the whole source code) and how can I manage this file? FYI: I use httpclient (http://loopj.com/android-async-http/).
最满意答案
您可以为此使用SharedPreferences接口。 请参阅Android SharedPreferences文档以了解它。
从Android文档:
SharedPreferences类提供了一个通用框架,允许您保存和检索原始数据类型的持久键值对。 您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长整数和字符串。 这些数据将在用户会话中持续存在(即使您的应用程序被终止)。
您可以按照如何在Android中使用SharedPreferences来存储,获取和编辑值以及sharedpreferences教程 ,以获取如何使用它的示例。
You can use SharedPreferences interface for this. See Android SharedPreferences doc to understand it.
From Android doc:
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
You may follow How to use SharedPreferences in Android to store, fetch and edit values and sharedpreferences tutorial for examples on how to use it.
更多推荐
发布评论