2012-06-23 Santiago Munin <burning1@gmail.com>

* 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
This commit is contained in:
santimunin 2012-06-23 14:28:37 +00:00
parent af1bfb2a6a
commit 167264165b
5 changed files with 63 additions and 71 deletions

View File

@ -1,3 +1,9 @@
2012-06-23 Santiago Munín <burning1@gmail.com>
* 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 <burning1@gmail.com> 2012-06-21 Santiago Munín <burning1@gmail.com>
* src/pandroid_event_viewer/pandorafms/Core.java: Added a new method which checks if a website is online. * 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. * src/pandroid_event_viewer/pandorafms/Options.java: Fixed a bug. The app didn't check if the url were online before checking the certificate.

View File

@ -113,4 +113,29 @@ public class API {
parameters.add(new BasicNameValuePair("other", other)); parameters.add(new BasicNameValuePair("other", other));
return Core.httpGet(context, parameters); return Core.httpGet(context, parameters);
} }
/**
* Get tags through an api call.
*
* @return A list of groups.
*/
public static List<String> getTags(Context context) {
ArrayList<String> array = new ArrayList<String>();
try {
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
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;
}
} }

View File

@ -445,7 +445,6 @@ public class Core {
HttpsURLConnection con; HttpsURLConnection con;
try { try {
con = (HttpsURLConnection) url.openConnection(); con = (HttpsURLConnection) url.openConnection();
con.getResponseCode();
con.connect(); con.connect();
con.disconnect(); con.disconnect();
return true; 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) { public static boolean isOnline(URL url) {
HttpsURLConnection con;
try { try {
con = (HttpsURLConnection) url.openConnection(); HttpsURLConnection con = (HttpsURLConnection) url
.openConnection();
con.setHostnameVerifier(new HostnameVerifier() { con.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
} }
}); });
con.setSSLSocketFactory(getSocketFactory()); con.setSSLSocketFactory(getSocketFactory());
if (con.getResponseCode()!=400) { con.setDoOutput(true);
return false; con.getInputStream();
} else return true;
} catch (IOException e) {
return true; return true;
} catch (IOException e) {
return false;
} }
} }
public static String httpsGet(String url, /**
List<NameValuePair> 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<NameValuePair> parameters) {
String result = ""; String result = "";
try { try {
HttpsURLConnection con = (HttpsURLConnection) new URL(url) HttpsURLConnection con = (HttpsURLConnection) new URL(url)
@ -487,7 +500,7 @@ public class Core {
con.setDoOutput(true); con.setDoOutput(true);
String postData = ""; String postData = "";
boolean first = true; boolean first = true;
for (NameValuePair nameValuePair : additionalParameters) { for (NameValuePair nameValuePair : parameters) {
postData = first ? postData : postData + "&"; postData = first ? postData : postData + "&";
first = false; first = false;
postData += URLEncoder.encode(nameValuePair.getName()) + "=" postData += URLEncoder.encode(nameValuePair.getName()) + "="

View File

@ -23,12 +23,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; 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 org.apache.http.message.BasicNameValuePair;
import android.app.Activity; 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<String> getTags() {
ArrayList<String> array = new ArrayList<String>();
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<NameValuePair> parameters = new ArrayList<NameValuePair>();
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. * Async task which get tags.
* *
@ -347,7 +291,7 @@ public class Main extends Activity {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
list = getTags(); list = API.getTags(getApplicationContext());
return null; return null;
} }

View File

@ -102,7 +102,6 @@ public class Options extends Activity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
save_options(); save_options();
new CheckConnectionAsyncTask().execute();
} }
}); });
@ -225,12 +224,14 @@ public class Options extends Activity {
this.getString(R.string.config_update_succesful_str), this.getString(R.string.config_update_succesful_str),
Toast.LENGTH_SHORT); Toast.LENGTH_SHORT);
toast.show(); toast.show();
new CheckConnectionAsyncTask().execute();
} else { } else {
Toast toast = Toast.makeText(context, Toast toast = Toast.makeText(context,
this.getString(R.string.config_update_fail_str), this.getString(R.string.config_update_fail_str),
Toast.LENGTH_LONG); Toast.LENGTH_LONG);
toast.show(); toast.show();
} }
} }
/** /**
@ -316,9 +317,12 @@ public class Options extends Activity {
private class CheckCertificateAsyncTask extends private class CheckCertificateAsyncTask extends
AsyncTask<URL, Void, Boolean> { AsyncTask<URL, Void, Boolean> {
private URL url; private URL url;
private boolean online;
@Override @Override
protected Boolean doInBackground(URL... arg0) { protected Boolean doInBackground(URL... arg0) {
url = arg0[0]; url = arg0[0];
online = Core.isOnline(url);
return Core.isValidCertificate(arg0[0]); return Core.isValidCertificate(arg0[0]);
} }
@ -326,7 +330,7 @@ public class Options extends Activity {
@Override @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
retrievingCertificate.dismiss(); retrievingCertificate.dismiss();
if (!Core.isOnline(url)) { if (!online) {
writeChanges(); writeChanges();
} else { } else {
if (!result) { if (!result) {