如何使用xmlpullparser提供rss

时间:2014-05-09 10:45:55

标签: android rss

我正在使用Xmlpullprser,但我没有获得价值。这是我的代码

  public class Newsfeed extends ListActivity{
 private HandleXML obj;
 private ProgressDialog pDialog;
 private String title = "title";
 private String description = "description";
 private String link = "link";
 SimpleAdapter sd ;
 ListView lv1;
 private XmlPullParserFactory xmlFactoryObject;
 XmlPullParser myParser;
 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
 private String finalUrl="http://www.moneycontrol.com/rss/MCtopnews.xml";
 @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.newsmain);
      lv1 =getListView();
     new HandleXML().execute();
 }  

 class HandleXML extends AsyncTask<String, String, String>
    {
     @Override
        protected void onPreExecute() {

            super.onPreExecute();
            pDialog = new ProgressDialog(Newsfeed.this);
            pDialog.setMessage("loading.");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        try{
             URL url = new URL(finalUrl);
             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             conn.setReadTimeout(10000 /* milliseconds */);
             conn.setConnectTimeout(15000 /* milliseconds */);
             conn.setRequestMethod("GET");
             conn.setDoInput(true);
             // Starts the query
             conn.connect();
             InputStream stream = conn.getInputStream();
             xmlFactoryObject = XmlPullParserFactory.newInstance();
             myParser = xmlFactoryObject.newPullParser();
             myParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
             myParser.setInput(stream, null);

             stream.close();
          } catch (Exception e) {
          }

        int event;
          String text=null;
          HashMap<String, String> map;
          try {
             event = myParser.getEventType();
             while (event != XmlPullParser.END_DOCUMENT) {
             String name=myParser.getName();
             switch (event){
                case XmlPullParser.START_TAG:
                break;
                case XmlPullParser.TEXT:
                   text = myParser.getText();
                break;
                case XmlPullParser.END_TAG:
                   if(name.equals("title")){
                      title = text;
                   }
                   else if(name.equals("link")){    
                      link = text;
                   }
                   else if(name.equals("description")){
                      description = text;
                   }
                   else{
                   }
                   break;
             }       
             map= new HashMap<String, String>();
             map.put("title",title);
             map.put("des",description);
             mylist.add(map);

             event = myParser.next(); 
           }

          } catch (Exception e) {
             e.printStackTrace();
          }
          sd =new SimpleAdapter(getApplicationContext(),mylist,R.layout.news,new String[]{"title","des"},new int[]{R.id.head,R.id.content});
        return null;
    }

    protected void onPostExecute(String file_url) {
         lv1.setAdapter(sd);
         pDialog.dismiss();

    }

    }
 }

我在第一个TextView中输出“title”,在secon TextView中输出“Description”,只有单行。请帮我。提前谢谢。

0 个答案:

没有答案