diff options
author | Parménides GV <parmegv@sdf.org> | 2013-02-04 16:35:28 +0100 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2013-02-04 16:35:28 +0100 |
commit | d2bd18ef560d95974117604af899b3a9fcc16dab (patch) | |
tree | 96831726c943bcccb104584d4748c5972a43654e /src/se/leap/leapclient/ProviderAPI.java | |
parent | 7b9e22e765a1e5eda05ad121684e63c20ff5f049 (diff) |
Created ConfigHelper with static information such as sharedprefs keys,
the saveSharedPrefs method and an unimplemented rescueFromJSONException.
Next step: managing HttpsURLConnection for the
CertPathValidatorException.
Diffstat (limited to 'src/se/leap/leapclient/ProviderAPI.java')
-rw-r--r-- | src/se/leap/leapclient/ProviderAPI.java | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/src/se/leap/leapclient/ProviderAPI.java b/src/se/leap/leapclient/ProviderAPI.java index aefb87fc..40638180 100644 --- a/src/se/leap/leapclient/ProviderAPI.java +++ b/src/se/leap/leapclient/ProviderAPI.java @@ -5,11 +5,16 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLConnection; import java.util.Scanner; +import javax.net.ssl.HttpsURLConnection; + +import org.json.JSONException; +import org.json.JSONObject; + import android.app.IntentService; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; @@ -23,45 +28,45 @@ public class ProviderAPI extends IntentService { @Override protected void onHandleIntent(Intent task_for) { - Bundle task ; + Bundle task; System.out.println("onHandleIntent called"); - if(!(task = task_for.getBundleExtra("downloadJSONFiles")).isEmpty()) - { - String provider_key = "provider"; - String eip_service_key = "eip"; - String provider_json_url = (String) task.get(provider_key); - String eip_service_json_url = (String) task.get(eip_service_key); + if (!(task = task_for.getBundleExtra(ConfigHelper.downloadJsonFilesBundleExtra)).isEmpty()) { + String provider_json_url = (String) task.get(ConfigHelper.provider_key); + String eip_service_json_url = (String) task.get(ConfigHelper.eip_service_key); try { - getAndParseSharedPref(provider_key, provider_json_url); - getAndParseSharedPref(eip_service_key, eip_service_json_url); + JSONObject provider_json = getFromProvider(provider_json_url); + ConfigHelper.saveSharedPref(ConfigHelper.provider_key, provider_json); + JSONObject eip_service_json = getFromProvider(eip_service_json_url); + ConfigHelper.saveSharedPref(ConfigHelper.eip_service_key, eip_service_json); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); + } catch (JSONException e) { + ConfigHelper.rescueJSONException(e); } } } - private void getAndParseSharedPref(String shared_preferences_key, - String json_url) throws IOException { + private JSONObject getFromProvider(String json_url) throws IOException, JSONException { URL url = new URL(json_url); - HttpURLConnection urlConnection = (HttpURLConnection) url - .openConnection(); + String json_file_content = ""; + URLConnection urlConnection = null; + + if (url.getProtocol().equalsIgnoreCase("https")) { + urlConnection = (HttpsURLConnection) url.openConnection(); + } else if (url.getProtocol().equalsIgnoreCase("http")) { + urlConnection = (HttpURLConnection) url.openConnection(); + } + try { InputStream in = new BufferedInputStream( urlConnection.getInputStream()); - String json_file_content = new Scanner(in).useDelimiter("\\A") - .next(); - - SharedPreferences.Editor shared_preferences_editor = ProviderListActivity.shared_preferences - .edit(); - shared_preferences_editor.putString(shared_preferences_key, - json_file_content); - shared_preferences_editor.commit(); - System.out.println("Shared preferences updated: " + ProviderListActivity.shared_preferences.getString(shared_preferences_key, "Default")); + json_file_content = new Scanner(in).useDelimiter("\\A").next(); } finally { - urlConnection.disconnect(); + ((HttpURLConnection) urlConnection).disconnect(); } - + + return new JSONObject(json_file_content); } } |