【问题标题】:How to get Url from XML Pull Parser如何从 XML Pull Parser 获取 URL
【发布时间】:2012-12-31 18:24:45
【问题描述】:

我无法从收到的数据中提取 url。

数据是

<ASX VERSION="3.0"><TITLE>iROCK109 - Classic Rock And More!</TITLE><MOREINFO HREF="http://www.irock109.com/" /><COPYRIGHT>� iROCK109 - 2009</COPYRIGHT><ABSTRACT>Classic Rock n More!</ABSTRACT><ENTRY><TITLE>[128k] www.irock109.com</TITLE><REF HREF="http://cp6.digistream.info:20366/" /><AUTHOR>iROCK 109 - Classic Rock And More!</AUTHOR><ABSTRACT>DJ Hosted Classic Rock 60s through today, and requests 24/7</ABSTRACT>

我使用的代码是读取数据并从 REF 中提取 HREF

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            executeHttpGet();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
        //Method for http request

        public void executeHttpGet() throws Exception {

            BufferedReader in = null;

            try {

                System.out.println("ndkjsdak");
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();

                HttpProtocolParams.setUseExpectContinue(client.getParams(), false);


                request.setURI(new URI("http://www.irock109.com/streams/irock109-128mp3.asx"));

                //HttpResponse response = client.execute(request);

                HttpResponse response = null;

                boolean RetryConnection = true;
                int retrycount = 0;

                //retry request 5 times
                while(RetryConnection == true && retrycount < 5)
                {
                    try
                    {
                         response = client.execute(request);
                         RetryConnection = false;
                    }
                    catch(IOException e)
                    {
                        System.out.println("caught");
                        retrycount += 1;
                    }
                }

                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));

                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");

                while ((line = in.readLine()) != null)
                {
                    sb.append(line +NL);
                }
                in.close();
                String page=sb.toString();
                System.out.println("xml data "+page);

                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                 factory.setNamespaceAware(true);
                 XmlPullParser xpp = factory.newPullParser();

                 xpp.setInput( new StringReader (page) );
                 int eventType = xpp.getEventType();
                 while (eventType != XmlPullParser.END_DOCUMENT)
                 {
                  if(eventType == XmlPullParser.START_DOCUMENT)
                  {
                      System.out.println("Start document");
                  }
                  else if(eventType == XmlPullParser.START_TAG)
                  {
                      System.out.println("Start tag "+xpp.getName());
                  }
                   else if(eventType == XmlPullParser.END_TAG)
                  {
                      System.out.println("End tag "+xpp.getName());
                  }
                  else if(eventType == XmlPullParser.TEXT) 
                  {
                      System.out.println("Text "+xpp.getText());
                  }
                  eventType = xpp.next();
                 }
                 System.out.println("End document");
            }
            finally 
            {
                if (in != null) 
                {
                    try
                    {
                        in.close();
                    } 
                    catch (IOException e)
                        {
                        e.printStackTrace();
                    }
                }
            }
    }
}

如何从ref 获取href

【问题讨论】:

    标签: android xml http url xml-parsing


    【解决方案1】:

    试试这个

    try {
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser xpp = factory.newPullParser();
    
                xpp.setInput(new StringReader(xmlData));
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_DOCUMENT) {
                        System.out.println("Start document");
                    } else if (eventType == XmlPullParser.START_TAG) {
                        if(xpp.getName().equalsIgnoreCase("REF"))
                        {
                            System.out.println("REF Text = " + xpp.getAttributeValue(0));
                        }
                        System.out.println("Start tag " + xpp.getName());
                    } else if (eventType == XmlPullParser.END_TAG) {
                        System.out.println("End tag " + xpp.getName());
                    } else if (eventType == XmlPullParser.TEXT) {
                        System.out.println("Text  = " + xpp.getText());
    
                    }
                    eventType = xpp.next();
                }
                System.out.println("End document");
            } catch (Exception e) {
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      相关资源
      最近更新 更多