Use volley to load IDL.
authorkenstir <kenstir@gmail.com>
Thu, 3 Dec 2015 04:43:10 +0000 (23:43 -0500)
committerkenstir <kenstir@gmail.com>
Thu, 3 Dec 2015 04:43:10 +0000 (23:43 -0500)
Open-ILS/src/Android/core/src/org/evergreen_ils/auth/AuthenticatorActivity.java
Open-ILS/src/Android/core/src/org/evergreen_ils/globals/GlobalConfigs.java
Open-ILS/src/Android/core/src/org/evergreen_ils/net/VolleyWrangler.java

index ade4b17..af42162 100644 (file)
@@ -104,51 +104,25 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity {
         }
     }
 
-    private class FetchConsortiumsTask extends AsyncTask<String, Integer, String> {
-        protected String doInBackground(String... params) {
-            String url = params[0];
-            String result = null;
-            try {
-                Log.d(TAG, "fetching " + url);
-                result = Utils.fetchUrl(url);
-                //todo move json parsing to doInBackground
-            } catch (Exception e) {
-                Log.d(TAG, "error fetching", e);
-            }
-            return result;
-        }
-
-        protected void onPostExecute(String result) {
-            long duration_ms = System.currentTimeMillis() - start_ms;
-            Log.d(TAG, "task fetch took " + duration_ms + "ms");
-            handleLibrariesJSON(result);
-        }
-    }
-
     private void startTask() {
         start_ms = System.currentTimeMillis();
-        boolean use_volley = true;
-        if (use_volley) {
-            RequestQueue q = VolleyWrangler.getInstance(this).getRequestQueue();
-            StringRequest stringRequest = new StringRequest(Request.Method.GET, libraries_directory_json_url,
-                    new Response.Listener<String>() {
-                        @Override
-                        public void onResponse(String response) {
-                            long duration_ms = System.currentTimeMillis() - start_ms;
-                            Log.d(TAG, "volley fetch took " + duration_ms + "ms");
-                            handleLibrariesJSON(response);
-                        }
-                    },
-                    new Response.ErrorListener() {
-                        @Override
-                        public void onErrorResponse(VolleyError error) {
-                            showAlert(error.getMessage());
-                        }
-                    });
-            q.add(stringRequest);
-        } else {
-            new FetchConsortiumsTask().execute(libraries_directory_json_url);
-        }
+        RequestQueue q = VolleyWrangler.getInstance(this).getRequestQueue();
+        StringRequest stringRequest = new StringRequest(Request.Method.GET, libraries_directory_json_url,
+                new Response.Listener<String>() {
+                    @Override
+                    public void onResponse(String response) {
+                        long duration_ms = System.currentTimeMillis() - start_ms;
+                        Log.d(TAG, "volley fetch took " + duration_ms + "ms");
+                        handleLibrariesJSON(response);
+                    }
+                },
+                new Response.ErrorListener() {
+                    @Override
+                    public void onErrorResponse(VolleyError error) {
+                        showAlert(error.getMessage());
+                    }
+                });
+        q.add(stringRequest);
     }
 
     // returns true if this is the generic app, which needs a library spinner etc.
index 5f14076..6f3794d 100644 (file)
@@ -23,12 +23,20 @@ import android.content.Context;
 import android.content.pm.ApplicationInfo;
 import android.text.TextUtils;
 import android.util.Log;
+import com.android.volley.Request;
+import com.android.volley.RequestQueue;
+import com.android.volley.Response;
+import com.android.volley.toolbox.StringRequest;
+import org.evergreen_ils.net.VolleyWrangler;
 import org.evergreen_ils.searchCatalog.Organisation;
 import org.evergreen_ils.searchCatalog.SearchCatalog;
+import org.open_ils.idl.IDLException;
 import org.open_ils.idl.IDLParser;
 import org.opensrf.net.http.HttpConnection;
 import org.opensrf.util.OSRFObject;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.text.ParseException;
@@ -71,6 +79,9 @@ public class GlobalConfigs {
     private String collectionsRequest = "/opac/common/js/" + locale
             + "/OrgTree.js";
 
+    private Context context = null;
+    private long start_ms;
+
     private GlobalConfigs() {
     }
 
@@ -78,6 +89,8 @@ public class GlobalConfigs {
         Log.d(TAG, "getGlobalConfigs (url="+httpAddress+")");
         if (instance == null)
             instance = new GlobalConfigs();
+        if (context != null)
+            instance.context = context;
         if (context != null && isDebuggable == null)
             isDebuggable = (0 != (context.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
         return instance;
@@ -132,22 +145,29 @@ public class GlobalConfigs {
     }
 
     public void loadIDL() {
-
-        try {
-            Log.d(TAG, "loadIDL fetching " + httpAddress + IDL_FILE_FROM_ROOT);
-            long start_ms = System.currentTimeMillis();
-            InputStream in_IDL = Utils.getNetInputStream(GlobalConfigs.httpAddress + IDL_FILE_FROM_ROOT);
-            IDLParser parser = new IDLParser(in_IDL);
-            parser.setKeepIDLObjects(false);
-            Log.d(TAG, "loadIDL parse");
-            parser.parse();
-            long duration_ms = System.currentTimeMillis() - start_ms;
-            Log.d(TAG, "loadIDL parse took "+duration_ms+"ms");
-        } catch (Exception e) {
-            Log.w(TAG, "loadIDL parse error", e);
-        }
-
-        loadedIDL = true;
+        start_ms = System.currentTimeMillis();
+        RequestQueue q = VolleyWrangler.getInstance(context).getRequestQueue();
+        StringRequest stringRequest = new StringRequest(Request.Method.GET, httpAddress + IDL_FILE_FROM_ROOT,
+                new Response.Listener<String>() {
+                    @Override
+                    public void onResponse(String response) {
+                        long duration_ms = System.currentTimeMillis() - start_ms;
+                        Log.d(TAG, "volley fetch took " + duration_ms + "ms");
+                        InputStream in = new ByteArrayInputStream(response.getBytes());
+                        IDLParser parser = new IDLParser(in);
+                        parser.setKeepIDLObjects(false);
+                        Log.d(TAG, "loadIDL parse");
+                        try {
+                            parser.parse();
+                        } catch (Exception e) {
+                            Log.d(TAG, "loadIDL failed", e);
+                        }
+                        duration_ms = System.currentTimeMillis() - start_ms;
+                        Log.d(TAG, "loadIDL parse took "+duration_ms+"ms");
+                    }
+                },
+                null);
+        q.add(stringRequest);
     }
 
     public void addOrganization(OSRFObject obj, int level) {
index 57f242a..b634288 100644 (file)
@@ -13,7 +13,7 @@ import com.android.volley.toolbox.Volley;
  * Created by kenstir on 12/2/2015.
  */
 // code from http://developer.android.com/training/volley/
-// or SallyPort or VolleyWrapper
+// or SallyPort or VolleyPort or VolleyWrapper
 public class VolleyWrangler {
     private static VolleyWrangler mInstance;
     private RequestQueue mRequestQueue;