package com.humbughq.android;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

class AsyncPoller extends HumbugAsyncPushTask {

    public AsyncPoller(HumbugActivity humbugActivity) {
        super(humbugActivity);
    }

    public final void execute() {
        this.execute("api/v1/get_messages");
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {
            JSONObject response = new JSONObject(result);
            JSONArray objects = response.getJSONArray("messages");
            for (int i = 0; i < objects.length(); i++) {
                Log.i("json-iter", "" + i);
                Message message = new Message(objects.getJSONObject(i));
                this.that.messages.add(message);

                if (message.getType() == Message.STREAM_MESSAGE) {
                    this.that.tilepanel.addView(this.that
                            .renderStreamMessage(message));
                }
            }
        } catch (JSONException e) {
            Log.e("json", "parsing error");
            e.printStackTrace();
        }
        if (this.that.suspended) {
            Log.i("poll", "suspended, dying");
            return;
        }
        this.that.current_poll = new AsyncPoller(this.that);
        this.that.current_poll.execute();
    }
}
@@ -467,17 +467,50 @@
 esult);%0A
-%0A
+        if (result != null) %7B%0A    
         
@@ -523,24 +523,28 @@
             
+ 
+   
 JSONObject r
@@ -569,32 +569,36 @@
 Object(result);%0A
+    
             JSON
@@ -644,24 +644,28 @@
 messages%22);%0A
+    
             
@@ -721,24 +721,28 @@
             
+    
 Log.i(%22json-
@@ -757,16 +757,20 @@
 %22 + i);%0A
+    
         
@@ -842,32 +842,36 @@
                 
+   
+ 
 this.that.messag
@@ -888,16 +888,20 @@
 sage);%0A%0A
+    
         
@@ -971,32 +971,36 @@
                 
+    
 this.that.tilepa
@@ -1049,16 +1049,20 @@
         
+  
+  
 .renderS
@@ -1093,34 +1093,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -1133,16 +1133,20 @@
         
+ 
+   
 %7D catch 
@@ -1161,24 +1161,28 @@
 eption e) %7B%0A
+    
             
@@ -1209,24 +1209,28 @@
 ng error%22);%0A
+    
             
@@ -1246,24 +1246,113 @@
 ackTrace();%0A
+            %7D%0A        %7D else %7B%0A            Log.i(%22poll%22, %22got nothing from the server%22);%0A
         %7D%0A  

Handle null replies.