Rss Feed没有加载

时间:2015-02-16 16:18:57

标签: android rss-reader android-xmlpullparser

我正在为新闻网站创建一个带有Rss阅读器的应用程序,但是这个Feed似乎没有加载。 这是我的代码:

的活动:

public class Tsf extends Activity{
private String finalUrl="http://feeds.tsf.pt/TSF-Ultimas";
private HandleXML obj;
ArrayList<String> title,link;
ListView list_tsf;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    title= new ArrayList<String>(1);
    title.add(0, "nada");
    link= new ArrayList<String>();
    setContentView(R.layout.tsf);
    list_tsf= (ListView)findViewById(R.id.list_tsf);
    carregar();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this, 
            android.R.layout.simple_list_item_1,
            title);
    list_tsf.setAdapter(arrayAdapter);
}

public void onStart(){
    super.onStart();
   }
class AtualizarPostAsyncTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        obj = new HandleXML(finalUrl);
          obj.fetchXML();
          if(obj.parsingComplete){
              title=(ArrayList<String>) obj.getTitle();
              link= (ArrayList<String>) obj.getLink();
          }
          return null;
    }
}
public void carregar(){
    AtualizarPostAsyncTask tarefa= new AtualizarPostAsyncTask();
    tarefa.execute();


}


}

处理程序:

public class HandleXML {

   private ArrayList<String> title = new ArrayList<String>();    
   private ArrayList<String> link = new ArrayList<String>();

   private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = false;
public HandleXML(String url){
      this.urlString = url;    }
public ArrayList<String> getTitle(){
      return title;    }    
public ArrayList<String> getLink(){
      return link;    }
       public void parseXMLAndStoreIt(XmlPullParser myParser) {
      int event;
      String text=null;
      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.add(text);
               }
               else if(name.equals("link")){    
                  link.add( text);
               }

               else{
               }
               break;
         }       
         event = myParser.next(); 
       }
       parsingComplete = true;
      } catch (Exception e) {
         e.printStackTrace();
      }    }    public void fetchXML(){
       Thread thread = new Thread(new Runnable(){
           @Override
           public void run() {
               try {
                   URL url = new URL(urlString);
                   HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //                   conn.setReadTimeout(100000 /* milliseconds */); //                  conn.setConnectTimeout(150000 /* milliseconds */);
                   conn.setRequestMethod("GET");
                   conn.setDoInput(true);
                   // Starts the query
                   conn.connect();
                   InputStream stream = conn.getInputStream();
                   xmlFactoryObject = XmlPullParserFactory.newInstance();
                   XmlPullParser myparser = xmlFactoryObject.newPullParser();
                   myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                   myparser.setInput(stream, null);
                   parseXMLAndStoreIt(myparser);
                   stream.close();
               } catch (Exception e) {
      }
      }
      });
      thread.start();
          } }

我没有收到任何错误消息,但是没有加载Feed。这是logcat:

02-16 16:14:31.572: I/View(21351): Touch up dispatch to com.android.internal.view.menu.ActionMenuItemView{41f3b530 VFED..CL ...P....                  336,0-504,144 #7f050047 app:id/ic_tsf}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=106.368164, y[0]=70.923996, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19250456, downTime=19250367, deviceId=3, source=0x1002 }
02-16 16:14:31.573: V/Provider/Settings(21351): from db cache, name = sound_effects_enabled , value = 0
02-16 16:14:31.705: D/GraphicBuffer(21351): create handle(0x612cc398) (w:1088, h:1920, f:1)
02-16 16:14:31.709: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x61724858>
02-16 16:14:31.711: D/OpenGLRenderer(21351): finish <0x61724858>
02-16 16:14:31.714: D/ActivityThread(21351): ACT-AM_ON_PAUSE_CALLED ActivityRecord{41d1d960 token=android.os.BinderProxy@41d1d078 {com.example.segundocasopratico/com.example.segundocasopratico.MainActivity}}
02-16 16:14:31.726: D/ActivityThread(21351): ACT-PAUSE_ACTIVITY handled : 1 / android.os.BinderProxy@41d1d078
02-16 16:14:31.763: D/AbsListView(21351): checkAbsListViewlLogProperty get invalid command
02-16 16:14:31.765: D/dalvikvm(21351): create interp thread : stack size=128KB
02-16 16:14:31.765: D/dalvikvm(21351): create new thread
02-16 16:14:31.766: D/dalvikvm(21351): new thread created
02-16 16:14:31.766: D/dalvikvm(21351): update thread list
02-16 16:14:31.766: D/dalvikvm(21351): threadid=11: interp stack at 0x63778000
02-16 16:14:31.766: D/dalvikvm(21351): threadid=11: created from interp
02-16 16:14:31.766: D/dalvikvm(21351): start new thread
02-16 16:14:31.766: D/dalvikvm(21351): threadid=11: notify debugger
02-16 16:14:31.766: D/dalvikvm(21351): threadid=11 (AsyncTask #1): calling run()
02-16 16:14:31.767: D/dalvikvm(21351): create interp thread : stack size=128KB
02-16 16:14:31.767: D/dalvikvm(21351): create new thread
02-16 16:14:31.767: D/ActivityThread(21351): ACT-AM_ON_RESUME_CALLED ActivityRecord{41f58f08 token=android.os.BinderProxy@41f58638 {com.example.segundocasopratico/com.example.segundocasopratico.Tsf}}
02-16 16:14:31.767: V/PhoneWindow(21351): DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41f5a490 I.E..... R.....ID 0,0-0,0}
02-16 16:14:31.768: D/dalvikvm(21351): new thread created
02-16 16:14:31.768: D/dalvikvm(21351): update thread list
02-16 16:14:31.769: D/dalvikvm(21351): threadid=12: interp stack at 0x63898000
02-16 16:14:31.769: D/dalvikvm(21351): threadid=12: created from interp
02-16 16:14:31.769: D/dalvikvm(21351): start new thread
02-16 16:14:31.769: D/dalvikvm(21351): threadid=12: notify debugger
02-16 16:14:31.769: D/dalvikvm(21351): threadid=12 (Thread-2245): calling run()
02-16 16:14:31.772: V/PhoneWindow(21351): DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{41fb7220 com.example.segundocasopratico/com.example.segundocasopratico.Tsf,ident = 1}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41f5a490 V.E..... R.....ID 0,0-0,0}
02-16 16:14:31.773: D/ActivityThread(21351): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{41f58f08 token=android.os.BinderProxy@41f58638 {com.example.segundocasopratico/com.example.segundocasopratico.Tsf}}
02-16 16:14:31.790: D/ListView(21351): measureHeightOfChildren adapter=android.widget.ArrayAdapter@41fb5208, startPosition=0, endPosition=0, maxHeight=1701, this=android.widget.ListView{41f61f38 VFED.VC. ......I. 0,0-0,0 #7f050043 app:id/list_tsf}
02-16 16:14:31.818: D/GraphicBuffer(21351): create handle(0x6236a490) (w:1088, h:1920, f:1)
02-16 16:14:31.824: I/MaliEGL(21351): [Mali]window_type=1, is_framebuffer=0, errnum = 0
02-16 16:14:31.824: I/MaliEGL(21351): [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
02-16 16:14:31.824: I/MaliEGL(21351): [Mali]max_allowed_dequeued_buffers=3
02-16 16:14:31.825: D/GraphicBuffer(21351): close handle(0x6236a490) (w:1088 h:1920 f:1)
02-16 16:14:31.835: D/GraphicBuffer(21351): create handle(0x637737f8) (w:1088, h:1920, f:1)
02-16 16:14:31.840: D/OpenGLRenderer(21351): setViewport 1080x1920 <0x637748e8>
02-16 16:14:31.843: D/ListView(21351): mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{41f61f38 VFED.VC. ......ID 0,0-1080,144 #7f050043 app:id/list_tsf}
02-16 16:14:31.862: D/dalvikvm(21351): GC_FOR_ALLOC freed 491K (6676), 9% free 6011K/6584K, paused 15ms, total 15ms
02-16 16:14:31.864: D/ListView(21351): measureHeightOfChildren adapter=android.widget.ArrayAdapter@41fb5208, startPosition=0, endPosition=0, maxHeight=1701, this=android.widget.ListView{41f61f38 VFED.VC. .F....ID 0,0-1080,144 #7f050043 app:id/list_tsf}
02-16 16:14:31.867: D/ListView(21351): mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{41f61f38 VFED.VC. .F....ID 0,0-1080,144 #7f050043 app:id/list_tsf}
02-16 16:14:31.873: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:31.877: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:31.881: D/AbsListView(21351): onWindowFocusChanged: hasWindowFocus=true, this=android.widget.ListView{41f61f38 VFED.VC. .F....I. 0,0-1080,144 #7f050043 app:id/list_tsf}
02-16 16:14:31.882: V/InputMethodManager(21351): onWindowFocus: android.widget.ListView{41f61f38 VFED.VC. .F....I. 0,0-1080,144 #7f050043 app:id/list_tsf} softInputMode=272 first=true flags=#1810100
02-16 16:14:31.883: V/InputMethodManager(21351): START INPUT: android.widget.ListView{41f61f38 VFED.VC. .F....I. 0,0-1080,144 #7f050043 app:id/list_tsf} ic=null tba=android.view.inputmethod.EditorInfo@41fc9d90 controlFlags=#105
02-16 16:14:31.893: D/dalvikvm(21351): create interp thread : stack size=128KB
02-16 16:14:31.893: D/dalvikvm(21351): create new thread
02-16 16:14:31.893: D/dalvikvm(21351): new thread created
02-16 16:14:31.893: D/dalvikvm(21351): update thread list
02-16 16:14:31.893: D/dalvikvm(21351): threadid=13: interp stack at 0x643c4000
02-16 16:14:31.893: D/dalvikvm(21351): threadid=13: created from interp
02-16 16:14:31.893: D/ListView(21351): measureHeightOfChildren adapter=android.widget.ArrayAdapter@41fb5208, startPosition=0, endPosition=0, maxHeight=1701, this=android.widget.ListView{41f61f38 VFED.VC. .F....I. 0,0-1080,144 #7f050043 app:id/list_tsf}
02-16 16:14:31.893: D/dalvikvm(21351): start new thread
02-16 16:14:31.894: D/dalvikvm(21351): threadid=13: notify debugger
02-16 16:14:31.894: D/dalvikvm(21351): threadid=13 (OkHttp ConnectionPool): calling run()
02-16 16:14:31.895: D/ListView(21351): mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{41f61f38 VFED.VC. .F....ID 0,0-1080,144 #7f050043 app:id/list_tsf}
02-16 16:14:31.896: D/libc-netbsd(21351): getaddrinfo: feeds.tsf.pt get result from proxy >>
02-16 16:14:31.897: I/System.out(21351): [socket][0] connection feeds.tsf.pt/173.194.78.121:80;LocalPort=54495(0)
02-16 16:14:31.897: I/System.out(21351): [CDS]connect[feeds.tsf.pt/173.194.78.121:80] tm:90
02-16 16:14:31.900: D/Posix(21351): [Posix_connect Debug]Process com.example.segundocasopratico :80 
02-16 16:14:31.902: D/GraphicBuffer(21351): create handle(0x643be420) (w:1088, h:1920, f:1)
02-16 16:14:31.907: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:31.908: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:31.920: D/GraphicBuffer(21351): create handle(0x63771370) (w:1088, h:1920, f:1)
02-16 16:14:31.923: D/OpenGLRenderer(21351): Flushing caches (mode 0)
02-16 16:14:31.924: D/GraphicBuffer(21351): close handle(0x617242d0) (w:1088 h:1920 f:1)
02-16 16:14:31.924: D/GraphicBuffer(21351): close handle(0x5cdc11f8) (w:1088 h:1920 f:1)
02-16 16:14:31.925: D/GraphicBuffer(21351): close handle(0x612cc398) (w:1088 h:1920 f:1)
02-16 16:14:31.926: D/GraphicBuffer(21351): close handle(0x63771370) (w:1088 h:1920 f:1)
02-16 16:14:31.957: I/System.out(21351): [socket][/192.168.1.225:54495] connected
02-16 16:14:31.957: I/System.out(21351): [CDS]rx timeout:0
02-16 16:14:32.069: D/dalvikvm(21351): threadid=14: interp stack at 0x61825000
02-16 16:14:32.078: V/PhoneWindow(21351): DecorView setVisiblity: visibility = 4 ,Parent =ViewRoot{41d812e8 com.example.segundocasopratico/com.example.segundocasopratico.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41d25130 I.E..... R....... 0,0-1080,1920}
02-16 16:14:32.078: D/ActivityThread(21351): ACT-STOP_ACTIVITY_HIDE handled : 0 / android.os.BinderProxy@41d1d078
02-16 16:14:32.132: I/System.out(21351): [CDS]rx timeout:100
02-16 16:14:32.132: I/System.out(21351): [CDS]rx timeout:0
02-16 16:14:32.132: D/dalvikvm(21351): threadid=12: exiting
02-16 16:14:32.132: D/dalvikvm(21351): threadid=12: bye!
02-16 16:14:33.012: D/GraphicBuffer(21351): create handle(0x63771370) (w:1088, h:1920, f:1)
02-16 16:14:33.018: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.019: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.030: D/GraphicBuffer(21351): create handle(0x612cc398) (w:1088, h:1920, f:1)
02-16 16:14:33.036: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.037: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.054: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.056: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.071: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.072: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.087: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.088: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.103: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.104: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.119: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.120: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.135: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.137: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.152: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.153: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.168: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.170: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.185: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.187: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.200: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.202: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.217: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.218: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.233: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.235: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.250: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.251: D/OpenGLRenderer(21351): finish <0x637748e8>
02-16 16:14:33.265: D/OpenGLRenderer(21351): prepareDirty (0.00, 0.00, 1080.00, 1920.00) opaque 1 <0x637748e8>
02-16 16:14:33.267: D/OpenGLRenderer(21351): finish <0x637748e8>

我将不胜感激。

2 个答案:

答案 0 :(得分:0)

if(obj.parsingComplete){
// ...

if会在调用它时检查一次的条件。当您致电fetchXML()时,您正在创建新线程并立即从该方法返回。到那时,您的parsingComplete - 变量仍为false

我认为你错了,你认为if等待直到条件为真,但它不会。它会检查一次,然后继续使用剩下的代码。

要解决此问题,请不要在Handler.fetchXML() - 方法中创建新的主题。您从AsyncTask调用此方法,因此您已经在单独的帖子中。


另外,这个:

title=(ArrayList<String>) obj.getTitle();
link= (ArrayList<String>) obj.getLink();

只需重新设置您ArrayList使用的ListView,即可更新内容。您必须将条目添加到适配器:

arrayAdapter.addAll(obj.getTitle());

如果您无法使用addAll()(已在API级别11中添加),请迭代您的标题并逐个添加。


最后,您的urlStringnull并且(至少在您添加的代码中)从未设置过。


我也不会养成在代码中到处创建线程的习惯,并且始终确保方法名称概述了将启动新线程的事实(例如,通过命名它asyncXYZ()) 。 线程很难处理,你不希望它们到处都是。

我的最后建议是,如果您正在进行异步工作,请不要等待该工作完成,但要告诉启动工作的代码,您已完成。 Observer pattern在这里工作得很好(这里是Android Framework

的例子

答案 1 :(得分:0)

正如@LukasKnuth建议的那样,我不应该使用threadAsynkTask,但主要问题是列表视图未被通知背景中所做的更改所以我添加了

protected void onPostExecute(Void result){
    super.onPostExecute(result);

    arrayAdapter.notifyDataSetChanged();
}

Asynktask并且它正在工作。