开发者

Calling Numbers From Parsed-XML Strings in Android App

开发者 https://www.devze.com 2023-03-30 02:07 出处:网络
I have an android app that parses strings from this web URL and stores them into an array called phoneNumberList.Then, when the time comes, the phone\'s dialer is to be launched, carrying that phone n

I have an android app that parses strings from this web URL and stores them into an array called phoneNumberList. Then, when the time comes, the phone's dialer is to be launched, carrying that phone number with it. The problem is, the dialer is showing up blank. Any ideas why?

Here's my code to parse the XML file and store it in an array:

    try {
            URL url = new URL("http://dl.dropbox.com/u/38725067/webexample/Site%203/index.xml");
            URLConnection conn = url.openConnection();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(conn.getInputStream());

            NodeList nodes = doc.getElementsByTagName("phone");
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);

                NodeList title = element.getElementsByTagName("string");
                Element line = (Element) title.item(0);
                phoneNumberList[i] = getCharacterDataFromElement(line);


            }
    }

... and here's my code that launches the dialer:

    public void launchDialer(String number){
        String numberToDial = "tel:"+number;
        startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numberToDial)));
    }

    public void onClick(View v) {
        String numberToCall = "0";
        switch (v.getId()) {
        case R.id.police_button1:
            numberTo开发者_运维百科Call = Main.phoneNumberList[0];
            launchDialer(numberToCall);
            break;
        case R.id.police_button2:
            launchDialer(Main.phoneNumberList[1]);
            break;

        }
    }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note 1: In the URL, you will see I attempted two formats for phone numbers: one with dashes, one without; neither worked.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note 2: In the latter code snippet, you can see I tried two different ways of calling the number: one using the string numberToCall, and one without the string; neither worked.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thank you in advanced for any and all help -

Mike Gates


Maybe getCharacterDataFromElement(line) is not returning the correct data. I didn't find this function so I replaced it with line.getTextContent()

In addition there also could exists a problem with your arrays. I would recommend using an ArrayList. This structure automatically resizes when you add a new item. Example code:

// global definition
private ArrayList<String> phoneNumberList = new ArrayList<String>();

// parsing numbers
NodeList nodes = doc.getElementsByTagName("phone");
for (int i = 0; i < nodes.getLength(); i++) {
    Element element = (Element) nodes.item(i);
    NodeList title = element.getElementsByTagName("string");
    Element line = (Element) title.item(0);
    phoneNumberList.add(line.getTextContent());
}

// calling number #0
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phoneNumberList.get(0))));
0

精彩评论

暂无评论...
验证码 换一张
取 消