0

我正在尝试从 URL 解析 xml 数据,它在 android 2.3 上运行,但它在 ICE-cream 三明治及更高版本上崩溃。谁可以帮我这个事?提前致谢。

XMLFunctions.class 中的代码

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLDecoder;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;


public class XMLFunctions {

    public final static Document XMLfromString(String xml){

        Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            is.setEncoding("UTF-8");
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            Log.e("XMLFunctions", "XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("XMLFunctions", "Wrong XML file structure: " + e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("XMLFunctions", "I/O exeption: " + e.getMessage());
            return null;
        }

        return doc;

    }

    /** Returns element value
      * @param elem element (it is XML tag)
      * @return Element value otherwise empty String
      */
     public final static String getElementValue( Node elem ) {
         Node kid;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                     if( kid.getNodeType() == Node.TEXT_NODE  ){
                         return kid.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }


     public static String getXML(String xmlURL){     
            String line = null;

            try {

                DefaultHttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(xmlURL);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                line = EntityUtils.toString(httpEntity);

            } catch (UnsupportedEncodingException e) {
                line = "<items count=\"0\" status=\"error\"></items>";
            } catch (MalformedURLException e) {
                line = "<items count=\"0\" status=\"error\"></items>";
            } catch (IOException e) {
                line = "<items count=\"0\" status=\"error\"></items>";
            }

            return line;

    }

    public static String numResults(Document doc){      
        Node items = doc.getDocumentElement();
        String res = "0";

        try{
            res = String.valueOf(items.getAttributes().getNamedItem("count").getNodeValue());
        }catch(Exception e ){
            res = "0";
        }

        return res;
    }

    public static String statusResult(Document doc){        
        Node items = doc.getDocumentElement();
        String res = "error";

        try{
            res = String.valueOf(items.getAttributes().getNamedItem("status").getNodeValue());
        }catch(Exception e ){
            res = "error";
        }

        return res;
    }

    public static String getValue(Element item, String str) {    

        NodeList n = item.getElementsByTagName(str);        
        return ParseXMLMethods.getElementValue(n.item(0));

      //  NodeList n = ((Document) e).getElementsByTagName(str);      
       // return XMLFunctions.getElementValue(n.item(0));
    }


    public static String urldecode(String stringToDecode){
        try {
            stringToDecode = URLDecoder.decode(stringToDecode, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return stringToDecode;
    }

}`

MainClass 是 XMLEntertainment.class

`

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class XMLEntertainment extends ListActivity {
    static final String KEY_ID="ItemID";
    static final String KEY_Image = "ImageURL";
    static final String KEY_Video = "VideoURL";
    static final String KEY_Audio = "AudioURL";
    static final String KEY_StartDate = "StartDate";
    static final String KEY_EndDate = "EndDate";
    static final String KEY_ItemName = "ItemName";
    static final String KEY_Address = "Address";
    static final String KEY_Suburb = "Suburb";
    static final String KEY_PostCode= "Postcode";
    static final String KEY_Longitude = "Longitude";
    static final String KEY_Latitude = "Latitude";
    static final String KEY_SubtypeName = "SubtypeName";
    static final String KEY_Cost = "Cost";
    static final String KEY_Details = "Details";
    static final String KEY_Website = "Website";
    static final String KEY_MajorRegionName = "MajorRegionName";
    static final String KEY_Phone = "Phone";
    static final String KEY_Email = "Email";
    static final String KEY_OpeningHours = "OpeningHours";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_entertainment);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        String xmlURL = "myURL Link";
        String xml = XMLFunctions.getXML(xmlURL);
       Document doc = XMLFunctions.XMLfromString(xml);

       // String xml = ParseXMLOutings.getXML();
       // Document doc = ParseXMLOutings.XMLfromString(xml);

        /*
        int numResults = XMLFunctions.numResults(doc);

        if((numResults <= 0)){
            Toast.makeText(ParseXMLDemo.this, "There is no data in the xml file", Toast.LENGTH_LONG).show();  
            finish();
        }
        */

        NodeList children = doc.getElementsByTagName("ListingElement");

        for (int i = 0; i < children.getLength(); i++) {                            
            HashMap<String, String> map = new HashMap<String, String>();    
            Element e = (Element)children.item(i);
            map.put(KEY_ID, XMLFunctions.getValue(e, KEY_ID));
            map.put(KEY_Image, XMLFunctions.getValue(e, KEY_Image));
            map.put(KEY_Video, XMLFunctions.getValue(e, KEY_Video));
            map.put(KEY_Audio, XMLFunctions.getValue(e, KEY_Audio));
            map.put(KEY_StartDate, XMLFunctions.getValue(e, KEY_StartDate));
            map.put(KEY_EndDate, XMLFunctions.getValue(e, KEY_EndDate));
            map.put(KEY_ItemName, XMLFunctions.getValue(e, KEY_ItemName));
            map.put(KEY_Address, XMLFunctions.getValue(e, KEY_Address));
            map.put(KEY_Suburb, XMLFunctions.getValue(e, KEY_Suburb));
            map.put(KEY_PostCode, XMLFunctions.getValue(e, KEY_PostCode));
            map.put(KEY_Longitude, XMLFunctions.getValue(e, KEY_Longitude));
            map.put(KEY_Latitude, XMLFunctions.getValue(e, KEY_Latitude));
            map.put(KEY_SubtypeName, XMLFunctions.getValue(e, KEY_SubtypeName));
            map.put(KEY_Cost, XMLFunctions.getValue(e, KEY_Cost));
            map.put(KEY_Details, XMLFunctions.getValue(e, KEY_Details));
            map.put(KEY_Website, XMLFunctions.getValue(e, KEY_Website));
            map.put(KEY_MajorRegionName, XMLFunctions.getValue(e, KEY_MajorRegionName));
            map.put(KEY_Phone, XMLFunctions.getValue(e,  KEY_Phone));
            map.put(KEY_Email, XMLFunctions.getValue(e, KEY_Email));
            map.put(KEY_OpeningHours, XMLFunctions.getValue(e, KEY_OpeningHours));



            mylist.add(map);            
        }       



         ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_items, 
                 new String[] { KEY_ItemName, KEY_SubtypeName, KEY_Suburb, KEY_PostCode, KEY_Cost,KEY_Details,KEY_Website,
        KEY_Phone,KEY_Email,KEY_MajorRegionName,KEY_OpeningHours}, 
            new int[] { R.id.title, R.id.type});

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            //Toast.makeText(xmlOutings.this,o.get("TourDetails"), Toast.LENGTH_LONG).show(); 


                 // getting values from selected ListItem
                String imageURL = o.get(KEY_Image);
                 String ItemName = o.get(KEY_ItemName);
                 String AudioURL=o.get(KEY_Audio);
                 String  VideoURL= o.get(KEY_Video);
                String Details =o.get(KEY_Details);
                String type = o.get(KEY_SubtypeName);
                String openingHours = o.get(KEY_OpeningHours);
                String cost = o.get(KEY_Cost);
              // String image1 = in.getStringExtra(KEY_Media_URL);
                String phone= o.get(KEY_Phone);
                String email =o.get(KEY_Email);
                String website = o.get(KEY_Website);
                String address =o.get(KEY_Address);
                String suburb = o.get(KEY_Suburb);
                String postcode = o.get(KEY_PostCode);
               String lat = o.get(KEY_Latitude);
              String lng = o.get(KEY_Longitude);



                    // Starting new intent
                   Intent in = new Intent(getApplicationContext(), ExplorerActivity.class);
                 //  in.putExtra(KEY_Image, imageURL );
                   in.putExtra(KEY_ItemName, ItemName );
                   in.putExtra(KEY_Audio, AudioURL );
                  in.putExtra(KEY_Video, VideoURL );
                  in.putExtra(KEY_Details, Details );
                  in.putExtra(KEY_SubtypeName, type );
                  in.putExtra(KEY_OpeningHours, openingHours );
                  in.putExtra(KEY_Cost, cost );
                  in.putExtra(KEY_Email, email);
                  in.putExtra(KEY_Phone, phone );
                  in.putExtra(KEY_Website, website);
                  in.putExtra(KEY_Address, address );
                  in.putExtra(KEY_Suburb, suburb);
                  in.putExtra(KEY_PostCode, postcode );
                  in.putExtra(KEY_Latitude, lat );
                  in.putExtra(KEY_Longitude, lng);

                      startActivity(in);
            }
            private Drawable LoadImageFromWebOperations(String url)
            {
                  try{
                InputStream is = (InputStream) new URL(url).getContent();
                Drawable d = Drawable.createFromStream(is, "KEY_Image");
                return d;
              }catch (Exception e) {
                System.out.println("Exc="+e);
                return null;
              }
            }

        });
    }
}
4

3 回答 3

1

您应该在 ICS 版本的 Android 中使用AsyncTask进行网络操作。您不能在主线程或 UI 线程上进行网络操作。你应该NetworkOnMainThread在 logcat 上得到异常。

一个例子如下,

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected String doInBackground(URL... urls) {
         String line = null;
         try {

            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(xmlURL);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<items count=\"0\" status=\"error\"></items>";
        } catch (MalformedURLException e) {
            line = "<items count=\"0\" status=\"error\"></items>";
        } catch (IOException e) {
            line = "<items count=\"0\" status=\"error\"></items>";
        }

        return line;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         // do further operations like start parsing from here
     }
 }
于 2012-11-23T11:43:11.373 回答
1

如果没有看到您的错误堆栈跟踪,我们将无法为您提供帮助。

但根据你的说法it is working on android 2.3 but it crashes on ICE-cream sandwich and above.

我建议您使用AsyncTask进行 XML 文件解析。这是因为您在应用程序的主 UI 线程中解析 XML。

于 2012-11-23T11:43:36.760 回答
0

这个问题有两个解决方案。

1)不要在主 UIThread 中编写网络调用,为此使用异步任务。

2) 在 setContentView(R.layout.activity_main); 之后将以下代码写入 MainActivity 文件;

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

并将下面的 import 语句放入您的 java 文件中。

import android.os.StrictMode;
于 2012-11-23T11:46:57.617 回答