1

I'm creating an app with a Rss reader for a news site, but the feed doesn't seem to be loading. Here is my code:

Activity:

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();


}


}

Handler:

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();
          } }

I don't get any error message, but the feed is not loading. Here is the 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>

I would appreciate any help.

Rasmnev
  • 109
  • 2
  • 11
  • After analysing the logcat, I think the problem is it reachs a timeout for some reason... `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` – Rasmnev Feb 16 '15 at 21:45

2 Answers2

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

The if will check it's condition once, at the point when it is called. When you call fetchXML(), you're creating a new thread and returning from that method immediately. Your parsingComplete-variable will still be false by that time.

I think you mistake is, that you think the if will wait until the condition is true, but it won't. It'll check it once and then continue on with the rest of the code.

To solve this, don't create a new Thread in your Handler.fetchXML()-method. You're calling this method from an AsyncTask, so you're already in a separate thread.


Also, this:

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

Simply re-setting the ArrayLists, that are used by your ListView won't update the contents. You'll have to add the entries to the adapter:

arrayAdapter.addAll(obj.getTitle());

If you can't use addAll() (it was added in API Level 11), iterate your titles and add the one by one.


At last, your urlString is null and (at least in the code that you added) never set.


I would also not get into the habit of creating threads everywhere in your code, and always make sure that the method-names outline the fact that a new thread will be started (for example by naming it asyncXYZ()). Threads are difficult to deal with and you don't want them all over the place.

My last advice is, if you're doing asynchronous work, don't wait for that work to finish but tell the code that initiated the work, that you are done. The Observer pattern works well here (here is an example from the Android Framework)

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I just added the `AsyncTask` because it was not working and I thought it was caused by it being called on the main thread... – Rasmnev Feb 16 '15 at 20:33
  • If you see closely, the if statement only appears after the `Handler.fetchXML()`- method is called,so I don't think that is the problem... – Rasmnev Feb 16 '15 at 21:09
  • I updated my answer. If you have any further problems, please take the time to format your code in the question. – Lukas Knuth Feb 17 '15 at 12:43
  • The code was a not correctly displayed. I just edited it. so you can see better. The `urlString` is updated in the method` `public HandleXML(String url){ this.urlString = url; }` that is called when we call the handler. – Rasmnev Feb 17 '15 at 13:55
  • By some code manipulations,I found the code stops after this line ` HttpURLConnection conn = (HttpURLConnection) url.openConnection();` everything else is working fine. Even the contents are being updated with the code like this. – Rasmnev Feb 17 '15 at 13:57
  • Ok I see what you mean now by the `arrayAdapter.addAll(obj.getTitle());`. I setted a `onItemClickListener` and I get an error saying the adapter changed but the listView wasn't notified. I used your suggestion but I get the same error – Rasmnev Feb 17 '15 at 14:40
  • I found a way! you were right a shouldn't use a `thread` and a `asynkTask`, but the main problem was that the listview wasn't notified of the change. I added `arrayAdapter.notifyDataSetChanged()` to the `onPostExecute` method and it's working. thanks for your help – Rasmnev Feb 17 '15 at 15:35
0

As @LukasKnuth suggested, I shouldn't use a thread and a AsynkTask, but the main problem was that the listview wasn't notified of the changes done in the Background so I added

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

    arrayAdapter.notifyDataSetChanged();
}

to the Asynktask and it's working.

Rasmnev
  • 109
  • 2
  • 11