2013-01-17 60 views
0

我無法從我收到的數據中提取網址。如何從XML Pull Parser獲取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讀取數據並exrtrat 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(); 
        } 
       } 
      } 
    } 
} 

我怎樣才能從refhref

回答

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) { 
     }