]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/HttpCallExecutableRunnable.java
Make FonBot usable
[fonbot.git] / src / ro / ieval / fonbot / HttpCallExecutableRunnable.java
index 1f96b1322c038679869f7c00a03da47c395a9457..12b1014c672cf4f6e1b3a760e89227471313a58b 100644 (file)
@@ -8,7 +8,7 @@ import java.io.OutputStream;
 import java.net.URL;
 import java.util.Collection;
 
-import javax.net.ssl.HttpsURLConnection;
+import java.net.HttpURLConnection;
 import org.eclipse.jdt.annotation.Nullable;
 
 import android.content.Context;
@@ -16,8 +16,6 @@ import android.preference.PreferenceManager;
 import android.util.Base64;
 import android.util.Log;
 
-import com.google.android.gcm.GCMRegistrar;
-
 /*
  * Copyright © 2013 Marius Gavrilescu
  * 
@@ -38,7 +36,7 @@ import com.google.android.gcm.GCMRegistrar;
  */
 
 /**
- * ExecutableRunnable that makes a HTTPS call to the server and hands the response to a callback
+ * ExecutableRunnable that makes a HTTP(S) call to the server and hands the response to a callback
  *
  * @author Marius Gavrilescu <marius@ieval.ro>
  */
@@ -131,42 +129,9 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{
        }
 
        @Override
-       public void run() {
+       public void run(){
                try {
-                       final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
-                       final HttpsURLConnection conn=(HttpsURLConnection) url.openConnection();
-                       if(data!=null){
-                               conn.setDoOutput(true);
-                               conn.setFixedLengthStreamingMode(data.length);
-                       }
-                       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){
-                               if(callback!=null)
-                                       callback.onError(toNonNull(context.getString(user_or_password_not_set)));
-                               return;
-                       }
-
-                       conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
-                                       (user+':'+password).getBytes(), Base64.NO_WRAP));
-                       if(headers != null)
-                               for (final Header header : headers)
-                                       conn.setRequestProperty(header.name, header.value);
-                       conn.connect();
-                       if(data!=null){
-                               final OutputStream stream=conn.getOutputStream();
-                               stream.write(data);
-                               stream.close();
-                       }
-                       Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
-                       final String message=conn.getResponseMessage();
-                       if(message==null && callback != null)
-                               callback.onError(toNonNull(context.getString(no_response_returned_from_server)));
-                       else if(message != null && callback != null)
-                               callback.onResult(conn.getResponseCode(), message,
-                                               toNonNull(conn.getInputStream()));
-                       conn.disconnect();
+                       doRun();
                } catch (Exception e) {
                        e.printStackTrace();
                        if(callback != null)
@@ -175,4 +140,44 @@ public final class HttpCallExecutableRunnable extends ExecutableRunnable{
                                retry();
                }
        }
+
+       public void doRun() throws Exception{
+               final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
+               final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
+               conn.setReadTimeout(24*60*1000);//24 minutes
+               if(data!=null){
+                       conn.setDoOutput(true);
+                       conn.setFixedLengthStreamingMode(data.length);
+               }
+               final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null);
+               final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null);
+               if(user == null || password == null || user.length() == 0 || password.length() == 0){
+                       if(callback!=null)
+                               callback.onError(toNonNull(context.getString(user_or_password_not_set)));
+                       return;
+               }
+
+               conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString((user+':'+password).getBytes(), Base64.NO_WRAP));
+               if(headers != null)
+                       for (final Header header : headers)
+                               conn.setRequestProperty(header.name, header.value);
+               conn.connect();
+               if(data!=null){
+                       final OutputStream stream=conn.getOutputStream();
+                       stream.write(data);
+                       stream.close();
+               }
+               Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
+               String message=conn.getResponseMessage();
+               if(message==null && callback != null)
+                       callback.onError(toNonNull(context.getString(no_response_returned_from_server)));
+               else if(message != null && callback != null){
+                       if(message.charAt(message.length()-1) == ')')//message is (something)
+                               message=message.substring(1, message.length()-1);
+                       else if(message.charAt(0) == '(')//message is (something) something else
+                               message=message.substring(message.indexOf(')')+2);
+                       callback.onResult(conn.getResponseCode(), message, conn.getResponseCode() == 200 ? conn.getInputStream() : null);
+               }
+               conn.disconnect();
+       }
 }
This page took 0.022482 seconds and 4 git commands to generate.