From d6d000a77b592532d619cd2f9aabf08a153862d3 Mon Sep 17 00:00:00 2001 From: santimunin Date: Sat, 23 Jun 2012 14:28:37 +0000 Subject: [PATCH] 2012-06-23 Santiago Munin * src/pandroid_event_viewer/pandorafms/Core.java: Fixed a bug in the isOnline() function. * src/pandroid_event_viewer/pandorafms/Options.java: Improved the connection checking functionality. * src/pandroid_event_viewer/pandorafms/API.java: Added getTags(). * src/pandroid_event_viewer/pandorafms/Main.java: Now the tags are getted by API.getTags(). git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6683 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- extras/pandroid_event_viewer/ChangeLog | 6 ++ .../pandroid_event_viewer/pandorafms/API.java | 25 ++++++++ .../pandorafms/Core.java | 33 +++++++---- .../pandorafms/Main.java | 58 +------------------ .../pandorafms/Options.java | 12 ++-- 5 files changed, 63 insertions(+), 71 deletions(-) diff --git a/extras/pandroid_event_viewer/ChangeLog b/extras/pandroid_event_viewer/ChangeLog index ed694ae653..04801cd682 100644 --- a/extras/pandroid_event_viewer/ChangeLog +++ b/extras/pandroid_event_viewer/ChangeLog @@ -1,3 +1,9 @@ +2012-06-23 Santiago Munín + * src/pandroid_event_viewer/pandorafms/Core.java: Fixed a bug in the isOnline() function. + * src/pandroid_event_viewer/pandorafms/Options.java: Improved the connection checking functionality. + * src/pandroid_event_viewer/pandorafms/API.java: Added getTags(). + * src/pandroid_event_viewer/pandorafms/Main.java: Now the tags are getted by API.getTags(). + 2012-06-21 Santiago Munín * src/pandroid_event_viewer/pandorafms/Core.java: Added a new method which checks if a website is online. * src/pandroid_event_viewer/pandorafms/Options.java: Fixed a bug. The app didn't check if the url were online before checking the certificate. diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java index 1f710aee39..894d2636ed 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java @@ -113,4 +113,29 @@ public class API { parameters.add(new BasicNameValuePair("other", other)); return Core.httpGet(context, parameters); } + + /** + * Get tags through an api call. + * + * @return A list of groups. + */ + public static List getTags(Context context) { + ArrayList array = new ArrayList(); + try { + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair("op", "get")); + parameters.add(new BasicNameValuePair("op2", "tags")); + parameters.add(new BasicNameValuePair("return_type", "csv")); + String return_api = Core.httpGet(context, parameters); + String[] lines = return_api.split("\n"); + array.add(""); + for (int i = 0; i < lines.length; i++) { + String[] tags = lines[i].split(";", 2); + array.add(tags[1]); + } + } catch (Exception e) { + Log.e(TAG, "getting tags problem"); + } + return array; + } } \ No newline at end of file diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java index 85ac708501..c586163348 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java @@ -445,7 +445,6 @@ public class Core { HttpsURLConnection con; try { con = (HttpsURLConnection) url.openConnection(); - con.getResponseCode(); con.connect(); con.disconnect(); return true; @@ -454,26 +453,40 @@ public class Core { } } + /** + * Checks if a url is online. + * + * @param url + * @return boolean + */ public static boolean isOnline(URL url) { - HttpsURLConnection con; try { - con = (HttpsURLConnection) url.openConnection(); + HttpsURLConnection con = (HttpsURLConnection) url + .openConnection(); con.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); con.setSSLSocketFactory(getSocketFactory()); - if (con.getResponseCode()!=400) { - return false; - } else return true; - } catch (IOException e) { + con.setDoOutput(true); + con.getInputStream(); return true; + } catch (IOException e) { + return false; } } - public static String httpsGet(String url, - List additionalParameters) { + /** + * Performs an secure http petition + * + * @param url + * Target + * @param parameters + * Petition parameters + * @return Result of the petition. + */ + private static String httpsGet(String url, List parameters) { String result = ""; try { HttpsURLConnection con = (HttpsURLConnection) new URL(url) @@ -487,7 +500,7 @@ public class Core { con.setDoOutput(true); String postData = ""; boolean first = true; - for (NameValuePair nameValuePair : additionalParameters) { + for (NameValuePair nameValuePair : parameters) { postData = first ? postData : postData + "&"; first = false; postData += URLEncoder.encode(nameValuePair.getName()) + "=" diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java index 65528b0277..70d53252c2 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java @@ -23,12 +23,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; @@ -285,57 +280,6 @@ public class Main extends Activity { } } - /** - * Get tags through an api call. - * - * @return A list of groups. - */ - private List getTags() { - ArrayList array = new ArrayList(); - - SharedPreferences preferences = getSharedPreferences( - this.getString(R.string.const_string_preferences), - Activity.MODE_PRIVATE); - - String url = preferences.getString("url", ""); - String user = preferences.getString("user", ""); - String password = preferences.getString("password", ""); - - try { - DefaultHttpClient httpClient = new DefaultHttpClient(); - - HttpPost httpPost = new HttpPost(url + "/include/api.php"); - - List parameters = new ArrayList(); - parameters.add(new BasicNameValuePair("user", user)); - parameters.add(new BasicNameValuePair("pass", password)); - parameters.add(new BasicNameValuePair("op", "get")); - parameters.add(new BasicNameValuePair("op2", "tags")); - parameters.add(new BasicNameValuePair("return_type", "csv")); - - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters); - - httpPost.setEntity(entity); - - HttpResponse response = httpClient.execute(httpPost); - HttpEntity entityResponse = response.getEntity(); - - String return_api = Core.convertStreamToString(entityResponse - .getContent()); - - String[] lines = return_api.split("\n"); - array.add(""); - for (int i = 0; i < lines.length; i++) { - String[] tags = lines[i].split(";", 2); - array.add(tags[1]); - } - } catch (Exception e) { - Log.e(TAG, "getting tags problem"); - } - - return array; - } - /** * Async task which get tags. * @@ -347,7 +291,7 @@ public class Main extends Activity { @Override protected Void doInBackground(Void... params) { - list = getTags(); + list = API.getTags(getApplicationContext()); return null; } diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java index 917cdff565..74a363e2af 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java @@ -102,7 +102,6 @@ public class Options extends Activity { @Override public void onClick(View v) { save_options(); - new CheckConnectionAsyncTask().execute(); } }); @@ -225,12 +224,14 @@ public class Options extends Activity { this.getString(R.string.config_update_succesful_str), Toast.LENGTH_SHORT); toast.show(); + new CheckConnectionAsyncTask().execute(); } else { Toast toast = Toast.makeText(context, this.getString(R.string.config_update_fail_str), Toast.LENGTH_LONG); toast.show(); } + } /** @@ -316,17 +317,20 @@ public class Options extends Activity { private class CheckCertificateAsyncTask extends AsyncTask { private URL url; + private boolean online; + @Override protected Boolean doInBackground(URL... arg0) { - url = arg0[0]; + url = arg0[0]; + online = Core.isOnline(url); return Core.isValidCertificate(arg0[0]); - + } @Override protected void onPostExecute(Boolean result) { retrievingCertificate.dismiss(); - if (!Core.isOnline(url)) { + if (!online) { writeChanges(); } else { if (!result) {