簡體   English   中英

從 Android 中的 URL 讀取 XML 文件

[英]reading XML File from URL in Android

我嘗試了幾種從鏈接打開 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 類

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();
        }

    }

我認為這個方法可以幫助你..放輕松

在此處輸入圖片說明

  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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM