2014-09-02 74 views
1

我目前正在爲android平臺製作rss feed閱讀應用程序,它適用於某些RSS提要;但不適用於其他人。特別是,它不適用於任何'天空'託管的RSS提要;但我不知道爲什麼,每當我嘗試使用天空網站的RSS源時,我都會收到上述錯誤消息。預期引用的字符串(位置:DOCDECL @ 1:50,[email protected]) - Android

RSS源像這樣的例子:

http://www.skysports.com/rss/0,20514,11661,00.xml

我不知道它是否是值得做的,我用我的XML解析?

有人請指出我正確的方向,我要去哪裏錯了嗎?

謝謝。

我的頻道頁:

package com.example.directrssread; 


import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import com.example.directrssread.Feed2.LongOperation; 
import com.google.android.gms.ads.AdView; 

import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.FragmentTransaction; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.support.v4.app.ListFragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

public class Feed1 extends ListFragment{ 

    String[] URL = new String[3]; 
    int count = 0; 
    String currURL = ""; 
    View mView; 
    AdView adView = null; 
    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 
    //Map<Date, ArrayList> menuItems = new TreeMap<Date, ArrayList>(); 
    Map<Date, ArrayList> sortedMap = null; 
    ProgressDialog progress; 

    //ProgressDialog.Builder builder; 
    // final ProgressDialog mDialog = new ProgressDialog((ViewPagerFragmentActivity)getActivity()); 
    // XML node keys 
    static final String KEY_ITEM = "item"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_NAME = "name"; 
    static final String KEY_TITLE = "title"; 
    static final String KEY_COST = "cost"; 
    static final String KEY_DESC = "description"; 
    static final String KEY_LINK = "link"; 
    static final String KEY_PUBDATE = "pubDate"; 
    static final String KEY_PUBTIME = "pubDate"; 
    ListView lv = null;//getListView(); 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 


     Log.e("Feed1", "Feed1"); 
     menuItems.removeAll(menuItems); 
     //lv = getListView(); 



     mView = inflater.inflate(R.layout.feed1, container, false); 
      return inflater.inflate(R.layout.feed1, container, false); 

     } 

    public void onActivityCreated(Bundle savedInstanceState) { 
     //menuItems.removeAll(menuItems); 
     count = 0;  
      //rssRun(); 
     lv = getListView(); 
     new LongOperation().execute(); 
      Log.e("Count", String.valueOf(menuItems.size())); 


     super.onActivityCreated(savedInstanceState); 

     } 

    public void rssRun() 
    { 

     Log.e("FEED1", "Running feed1"); 
     boolean runSubstring = true; 
          URL[0] = "http://www.football365.com/premier-league/rss"; 
      URL[1] = "http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk"; 
      URL[2] = "http://www.skysports.com/rss/0,20514,11661,00.xml"; 

      //URL[3] = "http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk"; 
      for (int f= 0;f < URL.length;f++) 
      { 

      try{ 
       //Log.e("TEst1", "TEst"); 

      XMLParser parser = new XMLParser(); 
      String xml = parser.getXmlFromUrl(URL[f]); // getting XML 
      Document doc = parser.getDomElement(xml); // getting DOM element 
      Log.e("HERE", "HERE"); 
      //Log.e("XML", xml); 

      NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
      //Log.e("NODELIST", nl.toString()); 
      // looping through all item nodes <item> 

      for (int i = 0; i < nl.getLength(); i++) { 
       // creating new HashMap 
       if (doc!=null) 
       { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       Element e = (Element) nl.item(i); 
       // adding each child node to HashMap key => value 
       if (count == 0) 
       { 
        currURL = "Football365"; 
       } 
       if (count == 1) 
       { 
        currURL = "BBC Sport"; 
       } 
       if (count == 2) 
       { 
        //currURL = "SkySports"; 
       } 

       //map.remove(map); 

       map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
       //Get the title of the article. 
       map.put(KEY_NAME, parser.getValue(e, KEY_TITLE)); 
       //Get the description of the article. 
       map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); 
       //Get the source e.g ' Football365'. 
       //String pubDate = parser.getValue(e, KEY_PUBDATE); 
       String pubDate = parser.getValue(e, KEY_PUBDATE); 
       //Parse the date and time from the main strings. 
       //Chop off the un-wanted parts. 
       //Bring them back together in one string. 
       String pubTime = pubDate.substring(17, pubDate.length()); 
       String pubDateFormat = ""; 
       pubTime = pubTime.substring(0, 5); 
       map.put(KEY_PUBDATE, pubTime); 

       pubDateFormat = pubDate.substring(0, 22); 
       pubDate = pubDate.substring(0, 16); 

       pubDate = pubDate + " " + pubTime; 


       SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm"); 
       Date date = dateFormatter.parse(pubDateFormat); 
       dateFormatter = new SimpleDateFormat("dd/MM/yyyy"); 
       //Log.e("DATE", dateFormatter.format(date)); 
       //Calendar c = Calendar.getInstance(); 
       //String currDate 
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
       String currDate = sdf.format(new Date()); 
       //Log.e("CURRENT DATE", currDate); 
       //System.out.println(dateFormatter.format(date)); 
       //System.out.println(sdf2.format(date)); 

       //Log.e("TITLE",)  
       map.put(KEY_TITLE, currURL + " - " + pubDate);   
       //Add the link. 
       map.put(KEY_LINK, parser.getValue(e, KEY_LINK)); 
       //Get the publish date. 
       //map.put(KEY_PUBDATE, parser.getValue(e, KEY_PUBDATE)); 
       // adding HashList to ArrayList 
       //10 
       //if (dateFormatter.format(date) == currDate) 
//    if (dateFormatter.format(date).substring(0, 10) == currDate.substring(0, 10)) 
       //String input = EditTextinput.getText().toString(); 
       //input = input.replace(" ", ""); 
       currDate = currDate.replace(" ", ""); 
       String rssDate = dateFormatter.format(date).replace(" ", ""); 
       Date dt1 = dateFormatter.parse(currDate); 
       Date dt2 = dateFormatter.parse(rssDate); 
       //Log.e("TRIM CURR", currDate); 
       //Log.e("RSSDATE", rssDate); 
       //if (rssDate == currDate) 
       if (dt1.compareTo(dt2)==0)//== currDate) 
       { 
        menuItems.add(map); 
        Collections.sort(menuItems, new MapComparator(KEY_PUBTIME)); 
        Collections.reverse(menuItems); 
        //sortedMap = new TreeMap<Date, ArrayList>(menuItems); 
        //Log.e("SAME", "THE SAME"); 
       } 
       //Log.e("TRIM CURR1", currDate); 
       //Log.e("RSSDATE1", rssDate); 
       //Log.e("TEst", "TEst"); 
       for (int q = 0;q < map.size();q++) 
       { 
        //Log.e("mene", map.get(KEY_TITLE)); 
       } 


       } 

      } 
      count+=1; 
      } 
      catch(Exception e) 
      { 
       Log.e("ERROR", e.toString()); 
      } 

      // Adding menuItems to ListView 
      final ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems, 
        R.layout.list_item, 
        new String[] { KEY_NAME, KEY_DESC,KEY_LINK, KEY_TITLE }, new int[] { 
          R.id.name, R.id.desciption, R.id.link, R.id.source}); 
      getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        setListAdapter(adapter); 
        //adapter.nnotifyDataSetChanged(); 
      //stuff that updates ui 

       } 
      }); 
      // selecting single ListView item 


      //TextView txt = getTextView(); 

      //TextView firstName = (TextView) V.findViewById(R.id.textView1); 

//   TextView firstName = (TextView) findViewById(R.id.textView1); 
      //ListView lv = (ListView) findViewById(R.id.l) 
      //ListView lv = ((ListView) findViewbyId(R.id) 

      lv.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        // getting values from selected ListItem 
        String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
        String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString(); 
        String link = ((TextView) view.findViewById(R.id.link)).getText().toString(); 
        String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString(); 

        // Starting new intent 
        Intent in = new Intent(getActivity().getApplication(), SingleMenuItemActivity.class); 
        in.putExtra(KEY_NAME, name); 
        in.putExtra(KEY_COST, cost); 
        in.putExtra(KEY_DESC, description); 
        in.putExtra(KEY_LINK, link); 
        startActivity(in); 

       } 
      }); 
     } 


     // mDialog.dismiss(); 
     Log.e("RUN", "RSSRUN"); 
    } 

    class LongOperation extends AsyncTask<String, Void, String> { 

     ProgressDialog progress1 = ProgressDialog.show(getActivity(), "dialog title", 
        "dialog message", true); 

      protected String doInBackground(String... params) { 


       menuItems.removeAll(menuItems); 
       try 
       { 
       rssRun(); 

       } 
       catch(Exception e) 
       { 
        menuItems.removeAll(menuItems); 
       } 

       getActivity().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         progress1.dismiss(); 
         //adapter.nnotifyDataSetChanged(); 
       //stuff that updates ui 

        } 
       }); 

       return currURL; 
      } 


      protected void onPostExecute(String result) { 


      } 
      protected void onPreExecute() { 
      } 


      protected void onProgressUpdate(Void... values) { 

      }  

     } 


    class MapComparator implements Comparator<Map<String, String>> 
    { 
     private final String key; 

     public MapComparator(String key) 
     { 
      this.key = key; 
     } 

     public int compare(Map<String, String> first, 
          Map<String, String> second) 
     { 
      // TODO: Null checking, both for maps and values 
      String firstValue = first.get(key); 
      String secondValue = second.get(key); 
      return ((String) first.get(key)).compareTo((String) second.get(key)); 

     } 
    } 

} 

我的XML解析器類:

package com.example.directrssread; 

import java.io.IOException; 
import java.io.StringReader; 
import java.io.UnsupportedEncodingException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

import android.util.Log; 

public class XMLParser { 

    // constructor 
    public XMLParser() { 

    } 

    /** 
    * Getting XML from URL making HTTP request 
    * @param url string 
    * */ 
    public String getXmlFromUrl(String url) { 
     String xml = null; 

     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      xml = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // return XML 
     return xml; 
    } 

    /** 
    * Getting XML DOM element 
    * @param XML string 
    * */ 
    public Document getDomElement(String xml){ 
     Document doc = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 

      DocumentBuilder db = dbf.newDocumentBuilder(); 
      dbf.setCoalescing(true); 
      if (db!=null) 
      { 
      InputSource is = new InputSource(); 
       is.setCharacterStream(new StringReader(xml)); 
       doc = db.parse(is); 
      } 

      } catch (ParserConfigurationException e) { 
       Log.e("Error: ", e.getMessage()); 
       return null; 
      } catch (SAXException e) { 
       Log.e("Error: ", e.getMessage()); 
       return null; 
      } catch (IOException e) { 
       Log.e("Error: ", e.getMessage()); 
       return null; 
      } 

      return doc; 
    } 

    /** Getting node value 
     * @param elem element 
     */ 
    public final String getElementValue2(Node elem) { 
     Node child; 
     if(elem != null){ 
      if (elem.hasChildNodes()){ 
       for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
        //if(child.getNodeType() == Node.TEXT_NODE ){ 
        if(child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE ){  
         return child.getNodeValue(); 
        } 
       } 
      } 
     } 
     return ""; 
     //return elem.getTextContent(); 
    } 

    /** 
     * Getting node value 
     * @param Element node 
     * @param key string 
     * */ 
    public String getValue(Element item, String str) {  
      NodeList n = item.getElementsByTagName(str);   
      return this.getElementValue2(n.item(0)); 
     } 


} 

確切的錯誤信息:

09-02 09:30:46.732: E/Error:(9578): Expected a quoted string (position:DOCDECL @1:50 in [email protected]) 

回答

0

看來,呼叫

parser.getXmlFromUrl(URL[f]) 

返回null,因爲在發生錯誤的情況下,它僅打印堆棧跟蹤並返回null,並且不會重新拋出異常。一般來說,不要處理這個錯誤是一個壞主意,因爲在這種情況下,你可能會在其他地方出現意想不到的行爲,這很難解釋。

+0

嗨,謝謝你的回覆。我明白了,那麼你認爲如何處理這個問題最好呢? – Callum 2014-09-02 09:13:05

+0

@Callum:我發現[這是一個很好的指南](http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/) 由於我無法處理IOException,UnsupportedEncodingException或ClientProtocolException我不會抓住他們,只是增加你的功能可能的例外。 – 2014-09-02 09:50:15

+0

好的,謝謝....我會看看這個! +1 – Callum 2014-09-02 10:05:51

相關問題