2014-09-02 3 views
1

현재 Android 플랫폼 용 RSS 피드 읽기 응용 프로그램을 만들고 있습니다. 일부 RSS 피드에서 작동합니다. 그러나 다른 사람들에게는 그렇지 않습니다. 특히, '하늘'호스팅 RSS 피드에는 작동하지 않습니다. 하지만 왜 하늘 웹 사이트에서 RSS 피드를 사용할 때마다 위의 오류 메시지가 표시되는지 확실하지 않습니다.인용 문자열 (위치 : [email protected]의 DOCDECL @ 1 : 50) - Android

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

나는 내가 사용하고 내 XML 구문 분석과 함께 할 수있는 뭔가인지 아닌지 확실하지 않다 :

RSS 예를 들어 다음과 같이 피드?

누군가 내가 잘못 가고있는 부분을 올바른 방향으로 가르쳐 주시겠습니까?

감사합니다.

내 피드 페이지 :

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]) 
,691 그

은 오류가 발생하는 경우 스택 추적을 인쇄하고 null을 반환하고 예외를 다시 throw하지 않기 때문에 null을 반환합니다. 일반적으로이 오류를 처리하지 않는 것은 좋지 않습니다. 왜냐하면이 경우에도 예기치 않은 동작이있는 것처럼 보이기 때문에 다른 곳에서는 설명하기가 훨씬 어려울 수 있기 때문입니다.

+0

안녕하세요, 응답 해 주셔서 감사합니다. 아 아아, 그래서 이것을 처리하는 가장 좋은 방법은 무엇이라고 생각하니? – Callum

+0

@Callum : [좋은 안내서] (http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/) IOException, UnsupportedEncodingException 또는 ClientProtocolException을 처리 할 수 ​​없어서 나는 그들을 잡아 내지 않을 것이고 당신의 기능에 가능한 예외를 추가 할 것이다. –

+0

아아, 고마워 .... 이걸 들여다 볼께! +1 – Callum

관련 문제