]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/PollResultCallback.java
Make FonBot usable
[fonbot.git] / src / ro / ieval / fonbot / PollResultCallback.java
index 2517fce29f8e65d04f8cb3963ff05612af770416..19da5bbabbdbb321177d15c2647501fdc11f251f 100644 (file)
@@ -3,6 +3,7 @@ package ro.ieval.fonbot;
 import static ro.ieval.fonbot.Utils.toNonNull;
 
 import java.io.InputStream;
+import org.eclipse.jdt.annotation.Nullable;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
@@ -53,19 +54,21 @@ final class PollResultCallback implements ResultCallback {
        }
 
        @Override
-       public void onResult(int responseCode, String responseMessage,
-                       InputStream inputStream) {
-               if(responseCode!=200)
+       public void onResult(final int responseCode, final String responseMessage, final @Nullable InputStream inputStream) {
+               if(responseCode != 200 || inputStream==null){
+                       if(responseCode != 204 && responseCode != 504)
+                               throw new RuntimeException("Bad HTTP response code: "+responseCode);
                        return;
+               }
 
                final Handler handler=new Handler(Looper.getMainLooper());
 
                try{
                        final JSONArray array;
                        {
-                               final byte[] buf=new byte[4096*1024];
-                               inputStream.read(buf);
-                               array=new JSONArray(new String(buf));
+                               final byte[] buf=new byte[2048*1024];
+                               final int length=inputStream.read(buf);
+                               array=new JSONArray(new String(buf, 0, length));
                        }
 
                        for(int i=0;i<array.length();i++){
@@ -73,7 +76,7 @@ final class PollResultCallback implements ResultCallback {
 
                                final JSONArray jsonargs=object.getJSONArray("args");
                                final String command=object.getString("command");
-                               final Address replyTo=new Address(toNonNull(Protocol.HTTP), object.getString("replyto"));//NOPMD address depends on command
+                               final Address replyTo=new Address(toNonNull(Protocol.HTTP), object.getString("replyto"), object.has("requestid") ? object.getString("requestid") : null);//NOPMD address depends on command
                                final String[] args=new String[jsonargs.length()];
                                for(int j=0;j<args.length;j++)
                                        args[j]=jsonargs.getString(j);
@@ -98,7 +101,7 @@ final class PollResultCallback implements ResultCallback {
 
        @Override
        public void onError(final String error) {
-               Log.e("PollResultCallback", "onError: "+error);
+               //error handling is done by the LongPollRunnable thread
        }
 
 }
This page took 0.025395 seconds and 4 git commands to generate.