我正在从 XML 文件中读取信息,它适用于 Android 2.3.6,但不适用于 4.2.2。有什么想法吗?
这是我在 MainActivity 中的代码
静态最终字符串 URL = " http://static.infomaniak.ch/vod/playlist/87/298/298_playlist.xml ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//LinearLayout layout = new LinearLayout(this);
TextView category[];
//ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
//Log.d("xml",xml);
Document doc = parser.getDomElement(xml); // getting DOM element
doc.normalize();
//NodeList nl = doc.getElementsByTagName("file");
NodeList nodeList = doc.getElementsByTagName("file");
category = new TextView[nodeList.getLength()];
String urls[] = new String[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
category[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList websiteList = fstElmnt.getElementsByTagName("file");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
category[i].setText(websiteElement.getAttribute("url"));
//layout.addView(category[i]);
urls[i]=category[i].getText().toString();
Log.d("urls[i]",category[i].getText().toString());
Toast.makeText(getApplicationContext(), category[i].getText().toString(), Toast.LENGTH_SHORT).show();
}
}
这是 XMLParser
公共类 XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
如果您检查 XML 它具有属性并且它是我需要的信息,我可以在 Android 2.3.6 中使用它,但该应用程序会在 4.2.2 中强制关闭 也许它发生在其他人身上,或者发生在其他人身上这个问题提前谢谢
问题是否可能是因为我正在使用 Action Bar Sherlock?