【问题标题】:How to read XML file with Android 4.2.2如何使用 Android 4.2.2 读取 XML 文件
【发布时间】:2013-05-21 16:11:48
【问题描述】:

我正在从 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??

【问题讨论】:

  • 什么错误,贴一下logcat
  • 它强制关闭,我不知道更多
  • 是的,您会在 logcat 中记录导致崩溃的原因。如果我们不知道错误以及它发生的位置,给我们代码将无济于事
  • 如果我在设备中安装应用程序,如何获取 logcat?
  • 先弄清楚为什么设备没有被识别,因为没有错误就没有这个问题的答案。此外,如果您将应用程序从计算机安装到设备,eclipse 显然已经认识到它的存在

标签: android xml-parsing xmlhttprequest


【解决方案1】:

问题出在网络调用上,我必须补充:

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

解决它

【讨论】:

    【解决方案2】:

    这段代码加在哪里?

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new     StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多