/** * 获取字符串id对应的url * * @param strId * @param context * @return url */ public static String getUrl(int strId, Context context) { try { XmlResourceParser parser = context.getResources().getXml(R.xml.address); AttributeSet attrs = Xml.asAttributeSet(parser); XmlUtils.beginDocument(parser, TAG_FTP); final int depth = parser.getDepth(); int type; while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { if (type != XmlPullParser.START_TAG) { continue; } final String name = parser.getName(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UrlAddress); if (TAG_URLADDRESS.equals(name)) { int id = a.getResourceId(R.styleable.UrlAddress_refId, 0); if (id == strId) { String url = a.getString(R.styleable.UrlAddress_address); a.recycle(); a = null; parser.close(); parser = null; return url; } } a.recycle(); } parser.close(); parser = null; } catch (XmlPullParserException e) { } catch (IOException e) { } return null; }
/** * 获取下载地址 配置文件在res/xml/address.xml * * @param context * @return */ public static HashMap<Integer, String> getUrlList(Context context) { try { XmlResourceParser parser = context.getResources().getXml(R.xml.address); AttributeSet attrs = Xml.asAttributeSet(parser); XmlUtils.beginDocument(parser, TAG_FTP); HashMap<Integer, String> urlMap = new HashMap<Integer, String>(8); final int depth = parser.getDepth(); int type; while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { if (type != XmlPullParser.START_TAG) { continue; } TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UrlAddress); final String name = parser.getName(); if (TAG_URLADDRESS.equals(name)) { UrlAddress address = getUrlAddress(a); if (address != null && address.mStringId > 0) { urlMap.put(address.mStringId, address.mUrl); } address = null; } a.recycle(); } parser.close(); parser = null; return urlMap; } catch (XmlPullParserException e) { } catch (IOException e) { } return null; }