如何在Android中获取RSS源

时间:2012-03-29 13:36:31

标签: android android-webview

public class SoccerActivity  extends Activity {

  private WebView webView;

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.soccer);

    webView = (WebView)findViewById(R.id.webview);

    String soccerString = QueryYahooSoccer();


    Document soccerDoc = convertStringToDocument(soccerString);


    String soccerResult = parseSoccerDescription(soccerDoc);

    webView.loadData(soccerResult, "text/html", "UTF-8");


  }

  private String parseSoccerDescription(Document srcDoc){


    String soccerDescription="";

    NodeList nodeListDescription = srcDoc.getElementsByTagName("description");

    if(nodeListDescription.getLength()>=0){

      for(int i=0; i<nodeListDescription.getLength(); i++){

        soccerDescription += nodeListDescription.item(i).getTextContent()+"<br/>";

      }

    }else{

      soccerDescription = ("No Description!");

    }

    return soccerDescription;

  }

  private Document convertStringToDocument(String src){

    Document dest = null;


    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder parser;


    try {
      parser = dbFactory.newDocumentBuilder();

      dest = parser.parse(new ByteArrayInputStream(src.getBytes()));


    } catch (ParserConfigurationException e1) {

      e1.printStackTrace();

      Toast.makeText(SoccerActivity.this, e1.toString(), Toast.LENGTH_LONG).show();

    } catch (SAXException e) {

      e.printStackTrace();

      Toast.makeText(SoccerActivity.this, e.toString(), Toast.LENGTH_LONG).show();

    } catch (IOException e) {

      e.printStackTrace();

      Toast.makeText(SoccerActivity.this, e.toString(), Toast.LENGTH_LONG).show();

    }

    return dest;

  }

  private String QueryYahooSoccer(){

    String qResult = "";

    String queryString = "http://news.yahoo.com/rss/soccer";

    HttpClient httpClient = new DefaultHttpClient();

    HttpGet httpGet = new HttpGet(queryString);

    try {
      HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

      if (httpEntity != null){

        InputStream inputStream = httpEntity.getContent();

        Reader in = new InputStreamReader(inputStream);

        BufferedReader bufferedreader = new BufferedReader(in);

        StringBuilder stringBuilder = new StringBuilder();

        String stringReadLine = null;

        while ((stringReadLine = bufferedreader.readLine()) != null) {

          stringBuilder.append(stringReadLine + "\n");

        }

        qResult = stringBuilder.toString();

      }

    } catch (ClientProtocolException e) {

      e.printStackTrace();

      Toast.makeText(SoccerActivity.this, e.toString(), Toast.LENGTH_LONG).show();

    } catch (IOException e) {

      e.printStackTrace();

      Toast.makeText(SoccerActivity.this, e.toString(), Toast.LENGTH_LONG).show();

    }

    return qResult;
  }

}

我创建的足球rss饲料如上面之后运行一个Android应用程序它只显示信息标题链接没有显示。问题是什么问题,你们身体知道请帮助我..

1 个答案:

答案 0 :(得分:0)

以下是完整的Documentexample code.