android上实现OMA下载

仔细研究了下OMA下载机制,把研究成果与大家分享。

 

网络处理层

package com.eshore.oma; import java.util.HashMap; import android.content.Context; import com.eshore.ezone.net.BaseAccessor; //网络层 public class OMAConnection { public static OMADescription parseOMAUrl(Context context, String url) { String[] arr = url.split("//?"); String urlString = arr[0]; HashMap<String, String> params = new HashMap<String, String>(); arr = arr[1].split("&"); for (int i = 0; i < arr.length; i++) { String[] string = arr[i].split("="); if (string.length == 2) { params.put(string[0], string[1]); } else { params.put(string[0], ""); } } return BaseAccessor.omaRequest(context, urlString, params); } public static void installNotify(Context context,String url){ String[] arr = url.split("//?"); String urlString = arr[0]; HashMap<String, String> params = new HashMap<String, String>(); arr = arr[1].split("&"); for (int i = 0; i < arr.length; i++) { String[] string = arr[i].split("="); if (string.length == 2) { params.put(string[0], string[1]); } else { params.put(string[0], ""); } } String result=BaseAccessor.deRequest(context, urlString, params); } }  

parserOMAUrl方法获得OMA下载描述文件

installNofiy方法在安装完成时将最终状态返回

 

 

 

模型层

package com.eshore.oma; public class OMADescription { public String objectURI; public String installNotifyURI; }  

objectURI是要下载对象的URI

installNotifyURI是安装完成时需要通知服务器的地址

 

 

将返回XML转化成OMA下载描述对象

package com.eshore.oma; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import com.eshore.ezone.util.Constants; import android.content.SharedPreferences; import android.util.Log; public class OMAParser { public static OMADescription parserString(String xml) { Log.d("dddd", xml); OMADescription omad = new OMADescription(); DocumentBuilderFactory docBuilderFactory = null; DocumentBuilder docBuilder = null; Document doc = null; try { docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilder = docBuilderFactory.newDocumentBuilder(); StringReader read = new StringReader(xml.replace('&', '*')); InputSource source = new InputSource(read); doc = docBuilder.parse(source); Element root = doc.getDocumentElement(); NodeList nodeList = root.getElementsByTagName("objectURI"); omad.objectURI = nodeList.item(0).getFirstChild().getNodeValue() .replace('*', '&'); NodeList nodeList2 = root.getElementsByTagName("installNotifyURI"); omad.installNotifyURI = nodeList2.item(0).getFirstChild().getNodeValue() .replace('*', '&'); } catch (Exception e) { e.printStackTrace(); } return omad; } } 

 

描述符的格式如下

<media xmlns="http://www.openmobilealliance.org/xmlns/dd"> <objectURI> http://xxx.xxxx.xxx/cot/20100409095238.apkID=035000000000000008320000000000000000001858 </objectURI> <size>123000</size> <type>application/x-cab-compressed</type> <name>dsds</name> <description>sss</description> <installNotifyURI> http://xxx.xxxx.xxx/report?ID=035000000000000008320000000000000000001858 </installNotifyURI> <nextURL>http://xxx.xxxx.xxx/portalapp</nextURL> </media>

你可能感兴趣的:(android,exception,xml,网络,String,Class)