Android Xml Parsing中的空指针异常

时间:2014-05-29 05:37:20

标签: android xml parsing

我正在开发一个Xml解析应用程序。从浏览器打开URL时,该URL不受信任。没有办法从服务器端纠正它。所以我使用了“trsut anybody”代码。 但我仍然得到一个空指针异常。 请帮我解决一下这个。 Thanx提前

这是我的代码。

public class SitesList {

    /** Variables */
    private ArrayList<String> From_Currency = new ArrayList<String>();
    private ArrayList<String> To_Currency = new ArrayList<String>();
    private ArrayList<String> exrt_buy = new ArrayList<String>();
    private ArrayList<String> exrt_sell = new ArrayList<String>();


    /** In Setter method default it will return arraylist 
     *  change that to add  */

    public ArrayList<String> getFrom_Currency() {
        return From_Currency;
    }

    public void setFrom_Currency(String From_Currency) {
        this.From_Currency.add(From_Currency);
    }

    public ArrayList<String> getTo_Currency() {
        return To_Currency;
    }

    public void setTo_Currency(String To_Currency) {
        this.To_Currency.add(To_Currency);
    }

    public ArrayList<String> getexrt_buy() {
        return exrt_buy;
    }

    public void setexrt_buy(String exrt_buy) {
        this.exrt_buy.add(exrt_buy);
    }


    public ArrayList<String> getexrt_sell() {
        return exrt_sell;
    }

    public void setexrt_sell(String exrt_sell) {
        this.exrt_sell.add(exrt_sell);
    }
}

MyXMLHandler类

public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static SitesList sitesList = null;

    public static SitesList getSitesList() {
        return sitesList;
    }

    public static void setSitesList(SitesList sitesList) {
        MyXMLHandler.sitesList = sitesList;
    }




    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;

        if (localName.equals("DOC"))
        {
            sitesList = new SitesList();
        }
        /*} else if (localName.equals("website")) {
            String attr = attributes.getValue("category");
            sitesList.setCategory(attr);
        }*/

    }


    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        if (localName.equalsIgnoreCase("From_Currency"))
            sitesList.setFrom_Currency(currentValue);
        else if (localName.equalsIgnoreCase("To_Currency"))
            sitesList.setTo_Currency(currentValue);
        else if (localName.equalsIgnoreCase("exrt_buy"))
            sitesList.setexrt_buy(currentValue);
        else if (localName.equalsIgnoreCase("exrt_sell"))
            sitesList.setexrt_sell(currentValue);

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }
}

XMLParsingExample类

public class XMLParsingExample extends Activity {

    /** Create Object For SiteList Class */
    SitesList sitesList = null;
     URL url;
     HttpsURLConnection https;
     HttpURLConnection conn = null;
     final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {

            return true;
        }
  };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



            try {
                url = new URL("https://222.165.187.91/ex_rate/XML_LOLC_EXRT.xml");
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if (url.getProtocol().toLowerCase().equals("https")) {
                trustAllHosts();

                try {
                    https = (HttpsURLConnection) url.openConnection();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                https.setHostnameVerifier(DO_NOT_VERIFY);
                conn = https;
            } else {
                try {
                    conn = (HttpURLConnection) url.openConnection();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView From_Currency[];
        TextView To_Currency[];
        TextView exrt_buy[];
        TextView exrt_sell[];

        try {

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL("https://222.165.187.91/ex_rate/XML_LOLC_EXRT.xml");

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Get result from MyXMLHandler SitlesList Object */
        sitesList = MyXMLHandler.sitesList;

        /** Assign textview array lenght by arraylist size */
        From_Currency = new TextView[sitesList.getFrom_Currency().size()];
        To_Currency = new TextView[sitesList.getTo_Currency().size()];
        exrt_buy = new TextView[sitesList.getexrt_buy().size()];
        exrt_sell = new TextView[sitesList.getexrt_sell().size()];

        /** Set the result text in textview and add it to layout */
        for (int i = 0; i < sitesList.getFrom_Currency().size(); i++) {
            From_Currency[i] = new TextView(this);
            From_Currency[i].setText("Name = "+sitesList.getFrom_Currency().get(i));

            To_Currency[i] = new TextView(this);
            To_Currency[i].setText("Website = "+sitesList.getTo_Currency().get(i));

            exrt_buy[i] = new TextView(this);
            exrt_buy[i].setText("Website Category = "+sitesList.getexrt_buy().get(i));

            exrt_sell[i] = new TextView(this);
            exrt_sell[i].setText("Website Category = "+sitesList.getexrt_sell().get(i));

            layout.addView(From_Currency[i]);
            layout.addView(To_Currency[i]);
            layout.addView(exrt_buy[i]);
            layout.addView(exrt_sell[i]);
        }

        /** Set the layout view to display */
        setContentView(layout);

    }

    private void trustAllHosts() {
          // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[] {};
                }


                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] arg0, String arg1)
                        throws java.security.cert.CertificateException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] arg0, String arg1)
                        throws java.security.cert.CertificateException {
                    // TODO Auto-generated method stub

                }
        } };

        // Install the all-trusting trust manager
        try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection
                                .setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
                e.printStackTrace();
        }

    }
}

0 个答案:

没有答案