仔細研究了下OMA下載機制,把研究成果與大家分享。
網路處理層
package com.eshore.oma;<br />import java.util.HashMap;<br />import android.content.Context;<br />import com.eshore.ezone.net.BaseAccessor;<br />//網路層<br />public class OMAConnection {<br />public static OMADescription parseOMAUrl(Context context, String url) {<br />String[] arr = url.split("//?");<br />String urlString = arr[0];<br />HashMap<String, String> params = new HashMap<String, String>();<br />arr = arr[1].split("&");<br />for (int i = 0; i < arr.length; i++) {<br />String[] string = arr[i].split("=");<br />if (string.length == 2) {<br />params.put(string[0], string[1]);<br />} else {<br />params.put(string[0], "");<br />}<br />}<br />return BaseAccessor.omaRequest(context, urlString, params);<br />}</p><p>public static void installNotify(Context context,String url){<br />String[] arr = url.split("//?");<br />String urlString = arr[0];<br />HashMap<String, String> params = new HashMap<String, String>();<br />arr = arr[1].split("&");<br />for (int i = 0; i < arr.length; i++) {<br />String[] string = arr[i].split("=");<br />if (string.length == 2) {<br />params.put(string[0], string[1]);<br />} else {<br />params.put(string[0], "");<br />}<br />}<br />String result=BaseAccessor.deRequest(context, urlString, params);<br />}<br />}<br />
parserOMAUrl方法獲得OMA下載描述檔案
installNofiy方法在安裝完成時將最終狀態返回
模型層
package com.eshore.oma;<br />public class OMADescription {<br />public String objectURI;<br />public String installNotifyURI;<br />}<br />
objectURI是要下載對象的URI
installNotifyURI是安裝完成時需要通知伺服器的地址
將返回XML轉化成OMA下載描述對象
package com.eshore.oma;<br />import java.io.StringReader;<br />import javax.xml.parsers.DocumentBuilder;<br />import javax.xml.parsers.DocumentBuilderFactory;<br />import org.w3c.dom.Document;<br />import org.w3c.dom.Element;<br />import org.w3c.dom.NodeList;<br />import org.xml.sax.InputSource;<br />import com.eshore.ezone.util.Constants;<br />import android.content.SharedPreferences;<br />import android.util.Log;<br />public class OMAParser {<br />public static OMADescription parserString(String xml) {<br />Log.d("dddd", xml);<br />OMADescription omad = new OMADescription();<br />DocumentBuilderFactory docBuilderFactory = null;<br />DocumentBuilder docBuilder = null;<br />Document doc = null;<br />try {<br />docBuilderFactory = DocumentBuilderFactory.newInstance();<br />docBuilder = docBuilderFactory.newDocumentBuilder();<br />StringReader read = new StringReader(xml.replace('&', '*'));<br />InputSource source = new InputSource(read);<br />doc = docBuilder.parse(source);<br />Element root = doc.getDocumentElement();<br />NodeList nodeList = root.getElementsByTagName("objectURI");<br />omad.objectURI = nodeList.item(0).getFirstChild().getNodeValue()<br />.replace('*', '&');<br />NodeList nodeList2 = root.getElementsByTagName("installNotifyURI");<br />omad.installNotifyURI = nodeList2.item(0).getFirstChild().getNodeValue()<br />.replace('*', '&');<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />return omad;<br />}<br />}
描述符的格式如下
<media xmlns="http://www.openmobilealliance.org/xmlns/dd"><br /><objectURI></p><p>http://xxx.xxxx.xxx/cot/20100409095238.apkID=035000000000000008320000000000000000001858</p><p></objectURI><br /><size>123000</size><br /><type>application/x-cab-compressed</type><br /><name>dsds</name><br /><description>sss</description><br /><installNotifyURI></p><p>http://xxx.xxxx.xxx/report?ID=035000000000000008320000000000000000001858</p><p></installNotifyURI><br /><nextURL>http://xxx.xxxx.xxx/portalapp</nextURL><br /></media>