Android读取网络配置(服务器配置)
时间:2011年06月23日作者:ronald查看次数:325 views评论次数:0
有很多时候,我们的应用和游戏需要从通过网络读取配置文件,文件一般是properties或xml. 我推荐使用properties的方式进行配置,读取文件很简单,不需要像xml那样,需要分析xml.
那什么情况下我们需要从服务器中读取配置文件呢?
1 ,根据网络的配置情况显示广告类型.因为当Android应用或游戏发布后,我们不能再修改应用中的配置,这时候我们只能从网络读取配置文件.
2 ,应用或游戏的版本信息.判断是否有新版本.如果有新版本,提示下载新版本.
3 ,应用事游戏的最新通知,新闻等等.
以上只是一般的情况,那当然还有很多的情况需要读取网络配置文件的.
当然,如果我们需要从网络读取配置文件的,需要服务器(能下载文件的服务)的支持.
以下是我的[ 足球即时比分] 应用中的一些代码.本人简单地封装了一些基数,提高重用性,方便其它应用或游戏再次使用.本代码是每天下载只会下载一次.每天生成一个文件,如果文件存在则不会从网络读取配置文件了.
以下是文件 AbstractNetConfigManager.java,是网络配置管理类的基类,提供主要的逻辑.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | package rbase.android.core.netconfig; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; import java.util.Properties; import rbase.android.core.util.DateTimeUtil; import rbase.android.core.util.IOUtil; import android.content.Context; import android.util.Log; /** * 抽象的网络配置管理类,用于当配置文件是一个URL的时候 * www.r-base.net by ronald */ public abstract class AbstractNetConfigManager<T> { protected boolean hasInit = false; protected T info; /** * 得到配置属性,当没有加载时,去网络读取文件进行加载. * * @param context * @return */ public T getNetConfigInfo(Context context) { if (hasInit && info != null) { return info; } // 进行初始化 init(context); if(info == null){ return getDefaultNetConfig(); } this.hasInit = true; return info; } /** * 得到配置属性,当没有加载时,读取本地的默认配置 * * @param context * @return */ public T getNetConfigInfoNotDownload(Context context) { if(!hasInit && info == null){ File configFile = getFileName(context); if(configFile.exists()){ info = readByFile(context, configFile); } if(info == null){ info = getDefaultNetConfig(); } } if(info == null){ return getDefaultNetConfig(); } return info; } /** * 生成配置文件的目录(文件名) * * @param context * @return */ private static File getFileName(Context context) { String fmtDate = DateTimeUtil.formatDate(new Date()); return new File(context.getFilesDir(), fmtDate + "_net-config.properties"); } /** * 从网络下载配置文件 * * @param context */ private void downloadPropertiesFromNet(Context context) { Log.i("www.r-base.net", "down net config from net,url:" + getNetConfigUrl()); FileOutputStream out = null; InputStream input = null; try { out = new FileOutputStream(getFileName(context)); input = new URL(getNetConfigUrl()).openStream(); IOUtil.copy(input, out); } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtil.close(out); IOUtil.close(input); } } /** * 初始化 * * @param context */ public void init(Context context) { // 判断是否需要从网络中下载xml文件 if (isNeedToUpdateFromNet(context)) { downloadPropertiesFromNet(context); } // 得到生成的文件目录 File file = getFileName(context); // 如果文件不存在,可能是下载文件问题,网络问题 if(!file.exists()){ return; } // 读取刚刚下载的文件 readByFile(context,file); } private T readByFile(Context context,File file){ FileInputStream input = null; try { input = new FileInputStream(file); Properties p = new Properties(); p.load(input); info = readNetConfigByProp(context,p); hasInit = true; } catch (IOException e) { e.printStackTrace(); if(file.exists() && file.canWrite()){ file.delete(); } return info; } finally{ IOUtil.close(input); } return null; } /** * 是否需要从网络下载配置文件 */ private static boolean isNeedToUpdateFromNet(Context context) { // 得到手机中的文件目录 File file = getFileName(context); boolean needUpdateFromNet = !file.exists(); Log.i("www.r-base.net", "need update site info from net : " + needUpdateFromNet+", file path : "+file.getAbsolutePath()); return needUpdateFromNet; } public abstract String getNetConfigUrl(); public abstract T getDefaultNetConfig(); public abstract T readNetConfigByProp(Context context, Properties p); } |
以下是使用网络配置的具体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /** * 网络配置类 * www.r-base.net by ronald */ public class NetConfigInfo{ public String configValue = "1"; // 默认值为1 } /** * 网络配置的实现类(具体类) * www.r-base.net by ronald */ public class NetConfigManager extends AbstractNetConfigManager<NetConfigInfo>{ private static final String NET_CONFIG_URL = "http://XX/net-config.properties"; private static final NetConfigManager instance = new NetConfigManager(); private NetConfigManager() { } public static final NetConfigManager getInstance() { return instance; } // 返回默认的配置类,当网络不能正常访问时会返回网络配置类 public NetConfigInfo getDefaultNetConfig() { return new NetConfigInfo(); } // 网络配置文件的URL public String getNetConfigUrl() { return NET_CONFIG_URL; } // 最重要的方法,读取文件的实现方法. public NetConfigInfo readNetConfigByProp(Context context, Properties p) { NetConfigInfo info = new NetConfigInfo(); info.configValue = p.getProperty("configValue", "1"); return info; } } |
相关资源 :
Android上传文件到服务器 : http://www.r-base.net/archives/360
返回 : Android开发博文汇总

最新评论
我在调出这个窗口的时候,暂停了我 »
Post:2011-11-29 19:31:02出8.0.5了,能否做一下破解呢? :mrgreen:
Post:2011-11-10 17:05:58:razz: 一直在找这个东西,甚至也 »
Post:2011-10-21 21:29:42Alarm 这个类是一个挺有意思的类, »
Post:2011-10-19 15:05:46回来看看等DbVisualizer 8.0 新版 »
Post:2011-10-10 19:20:21好东西,收藏了。
Post:2011-09-25 22:36:38:twisted: 不麻烦的话,能否能做 »
Post:2011-09-05 16:10:59你可以到hostloc去找IDC,很多。 X »
Post:2011-08-28 12:37:23