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>
* 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.

View File

@ -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<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;
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<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 = "";
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()) + "="

View File

@ -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<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.
*
@ -347,7 +291,7 @@ public class Main extends Activity {
@Override
protected Void doInBackground(Void... params) {
list = getTags();
list = API.getTags(getApplicationContext());
return null;
}

View File

@ -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<URL, Void, Boolean> {
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) {