]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/PollServerAsyncTask.java
Add ExecutableRunnable
[fonbot.git] / src / ro / ieval / fonbot / PollServerAsyncTask.java
index 15bf9cc093e6a0932b1bb568d38b2a2c25d074b0..07c014b0979c9245e17aa1f83b71b5917c6c9cdf 100644 (file)
@@ -21,6 +21,8 @@ import android.preference.PreferenceManager;
 import android.util.Base64;
 import android.util.Log;
 
+import com.google.android.gcm.GCMRegistrar;
+
 /*
  * Copyright © 2013 Marius Gavrilescu
  * 
@@ -98,7 +100,7 @@ public final class PollServerAsyncTask extends AsyncTask<Void, Void, List<PollSe
                try {
                        final URL url=Utils.getServerURL(toNonNull(context),"/get");
                        final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
-                       conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
+                       conn.setRequestProperty("X-ID", GCMRegistrar.getRegistrationId(context));
                        final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
                        final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
                        if(user == null || password == null)
@@ -107,24 +109,29 @@ public final class PollServerAsyncTask extends AsyncTask<Void, Void, List<PollSe
                        conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
                                        (user+':'+password).getBytes(), Base64.NO_WRAP));
                        conn.connect();
-                       final byte[] buf=new byte[4096*1024];
                        Log.d(getClass().getName(), "Server poll got response code "+conn.getResponseCode()+" and message "+conn.getResponseMessage());
                        if(conn.getResponseCode()!=200)
                                return commands;
-                       conn.getInputStream().read(buf);
-                       final JSONArray array=new JSONArray(new String(buf));
-                       final List<String> list=new ArrayList<String>();
+
+                       final JSONArray array;
+                       {
+                               final byte[] buf=new byte[4096*1024];
+                               conn.getInputStream().read(buf);
+                               array=new JSONArray(new String(buf));
+                       }
+
                        for(int i=0;i<array.length();i++){
                                final JSONObject object=array.getJSONObject(i);
 
-                               for(int j=0;object.has("arg"+j);j++)
-                                       list.add(object.getString("arg"+j));
+                               final JSONArray jsonargs=object.getJSONArray("args");
+                               final String[] args=new String[jsonargs.length()];
+                               for(int j=0;j<args.length;j++)
+                                       args[j]=jsonargs.getString(j);
 
                                commands.add(new Command(//NOPMD command changes for each JSON object
                                                toNonNull(object.getString("command")),
-                                               toNonNull(list.toArray(new String[list.size()])),
+                                               args,
                                                toNonNull(object.getString("replyto"))));
-                               list.clear();
                        }
                } catch (MalformedURLException e) {
                        e.printStackTrace();
@@ -146,7 +153,7 @@ public final class PollServerAsyncTask extends AsyncTask<Void, Void, List<PollSe
                        Log.d(getClass().getName(), "Poll got command "+command.command+" with "+((command.args.length==0)?"no args":"args "+Utils.join(
                                        " ",toNonNull(command.args))));
                        Utils.processCommand(
-                                       toNonNull(FonBotApplication.instance),
+                                       toNonNull(context),
                                        toNonNull(command.command),
                                        toNonNull(command.args),
                                        new Address(toNonNull(Protocol.HTTP), command.replyto));//NOPMD address depends on command
This page took 0.027123 seconds and 4 git commands to generate.