1

我尝试了几种从链接打开 XML 文件的方法,但所有的道路都没有任何好处。为了确保解析 XML 文件是好的,我下载了文件并将它放在 Asset 文件夹中,以便解析所有这些。我们可以由此推断出从 url 读取的错误

网址是http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk

HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url.toString());
HttpResponse response = httpclient.execute(httppost);
InputStream in=response.getEntity().getContent();

或者

HttpURLConnection con=(HttpURLConnection)url.openConnection();
InputStream in=con.getInputStream();

或者

InputStream in =url.openStream();

请帮我

我的问题解决了谢谢大家的帮助。当我从 Url 获取文件时,我应该使用 AsyncTask 类

4

2 回答 2

0
public void xmlReader() {

        try {

            URL url = new URL("http://feeds.bbci.co.uk/sport/0/football/rss.xml");

            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory
                    .newDocumentBuilder();
            Document document = documentBuilder.parse(new InputSource(url
                    .openStream()));

            nodlist = document.getElementsByTagName("Youritem");

            for (int i = 0; i < nodlist.getLength(); i++) {
                Element element = (Element) nodlist.item(i);

                NodeList nodlistid = element.getElementsByTagName("Id");
                Element id = (Element) nodlistid.item(0);


                NodeList nodlistBaslik = element.getElementsByTagName("tagname1");
                Element baslik = (Element) nodlistBaslik.item(0);

                NodeList nodlistDetay = element.getElementsByTagName("tagname2");
                Element detay = (Element) nodlistDetay.item(0);

                NodeList nodlistKaynak = element.getElementsByTagName("tagname3");
                Element lat = (Element) nodlistKaynak.item(0);

                NodeList nodelistMedia = element.getElementsByTagName("tagname4");
                Element longi = (Element) nodelistMedia.item(0);

                NodeList nodelistTur = element.getElementsByTagName("tagname5");
                Element tur = (Element) nodelistTur.item(0);
                // String resimURL =
                // resim.getAttributes().getNamedItem("url").getNodeValue();

                NodeList nodelistMedia1 = element.getElementsByTagName("enclosure");    
                Element picture= (Element) nodelistMedia1.item(0);
                String pictureURL = resim.getAttributes().getNamedItem("picturetagname").getNodeValue();    

                xmltagname1.add(tagname1.getChildNodes().item(0).getNodeValue());
                xmltagname2.add(tagname2.getChildNodes().item(0).getNodeValue());
                xmltagname3.add(tagname3.getChildNodes().item(0).getNodeValue());
                xmltagname4.add(tagname4.getChildNodes().item(0).getNodeValue());
                xmltagname5.add(tagname5.getChildNodes().item(0).getNodeValue());


            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

我认为这个方法可以帮助你.. 放轻松

于 2013-06-05T13:08:30.013 回答
0

在此处输入图像描述

  String TextHolder = "", TextHolder2 = "";
public class GetNotePadFileFromServer extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.show();
    }
    @Override
    protected Void doInBackground(Void... params) {
        String userId = SharedPrefHelper.getPrefsHelper().getPref(SharedPrefHelper.PREF_USER_ID);

        try {
            URL url = new URL(baseUrl+"fileName.xml");
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(url.openStream()));
            while ((TextHolder2 = bufferReader.readLine()) != null) {
                TextHolder += TextHolder2;
            }
            bufferReader.close();

        } catch (MalformedURLException malformedURLException) {
            malformedURLException.printStackTrace();
            TextHolder = malformedURLException.toString();

        } catch (IOException iOException) {
            iOException.printStackTrace();
            TextHolder = iOException.toString();
        }
        return null;

    }

    @Override
    protected void onPostExecute(Void finalTextHolder) {
        Document doc = convertStringToXMLDocument( TextHolder );
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName(ApiConstant.ApiKeys.SMS_MESSAGE);
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                Sms sms = new Sms(
                        eElement.getElementsByTagName(ApiConstant.ApiKeys.SMS_IDS).item(0).getTextContent(),
                        eElement.getElementsByTagName(ApiConstant.ApiKeys.SMS_ADDRESS).item(0).getTextContent(),
                        eElement.getElementsByTagName(ApiConstant.ApiKeys.SMS_BODY).item(0).getTextContent(),
                        eElement.getElementsByTagName(ApiConstant.ApiKeys.SMS_READ).item(0).getTextContent(),
                        eElement.getElementsByTagName(ApiConstant.ApiKeys.SMS_DATE).item(0).getTextContent(),
                        eElement.getElementsByTagName(ApiConstant.ApiKeys.SMS_TYPE).item(0).getTextContent(),
                        false
                );

                LogClass.e("mysms",""+sms.getAddress());
                smsArrayList.add(sms);
            }
        }

        setUi();
        progressDialog.dismiss();
        super.onPostExecute(finalTextHolder);
    }

}


  new GetNotePadFileFromServer().execute();
于 2019-11-12T09:30:13.810 回答