Use [Ion](https://github.com/koush/ion) for http requests.

This commit is contained in:
Tim Schumacher 2015-02-11 17:54:41 +01:00
parent da310a7e67
commit 37c0570067
5 changed files with 48 additions and 88 deletions

View file

@ -83,8 +83,12 @@
</content> </content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" /> <orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-21.0.2" level="project" /> <orderEntry type="library" exported="" name="androidasync-2.0.5" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.2" level="project" /> <orderEntry type="library" exported="" name="support-v4-21.0.2" level="project" />
<orderEntry type="library" exported="" name="gson-2.3" level="project" />
<orderEntry type="library" exported="" name="ion-2.0.5" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.2" level="project" />
<orderEntry type="library" exported="" name="AndroidPinning-1.0.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.2" level="project" /> <orderEntry type="library" exported="" name="appcompat-v7-21.0.2" level="project" />
</component> </component>
</module> </module>

View file

@ -30,4 +30,6 @@ android {
dependencies { dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.2' compile 'com.android.support:appcompat-v7:21.0.2'
compile 'org.thoughtcrime.ssl.pinning:AndroidPinning:1.0.0'
compile 'com.koushikdutta.ion:ion:2.+'
} }

View file

@ -1,63 +0,0 @@
/*******************************************************************************
* "THE VODKA-WARE LICENSE" (Revision 42):
* Tim <tim@datenknoten.me> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a vodka in return Tim Schumacher
******************************************************************************/
package me.datenknoten.tueroeffner;
import android.os.AsyncTask;
import android.widget.EditText;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by tim on 27.01.2015.
*/
public class CommandExecuter extends AsyncTask<String, Integer, Boolean> {
private String key = "";
public CommandExecuter(String Key) {
this.key = Key;
}
@Override
protected Boolean doInBackground(String... params) {
int count = params.length;
try {
for (String param : params) {
executeCommand(param);
// Escape early if cancel() is called
if (isCancelled()) break;
}
return true;
} catch (Exception e) {
return false;
}
}
/**
* Sends a command to the door server.
*
* @param cmd
*/
private Boolean executeCommand(String cmd) throws IOException, URISyntaxException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI uri = new URI("https://tuer.hackspace-jena.de/cgi-bin/kraut.space?secret="+key+"&cmd="+cmd);
request.setURI(uri);
HttpResponse response = client.execute(request);
return true;
}
}

View file

@ -21,6 +21,7 @@ import android.support.v4.app.Fragment;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -38,12 +39,18 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.thoughtcrime.ssl.pinning.PinningTrustManager;
import org.thoughtcrime.ssl.pinning.SystemKeyStore;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List; import java.util.List;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import javax.net.ssl.TrustManager;
public class MainActivity extends ActionBarActivity { public class MainActivity extends ActionBarActivity {
@ -181,12 +188,7 @@ public class MainActivity extends ActionBarActivity {
* @param v * @param v
*/ */
public void buttonOpenOuterDoor(View v) { public void buttonOpenOuterDoor(View v) {
try { executeCommand("outdoor_buzz",v.getContext());
new CommandExecuter(getDoorKey()).doInBackground("outdoor_buzz");
Toast.makeText(v.getContext(), getString(R.string.buzzer_success), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(v.getContext(), "Konnte Befehl „Buzzer“ nicht ausführen.", Toast.LENGTH_SHORT).show();
}
} }
/** /**
@ -195,12 +197,7 @@ public class MainActivity extends ActionBarActivity {
* @param v * @param v
*/ */
public void buttonOpenInnerDoor(View v) { public void buttonOpenInnerDoor(View v) {
try { executeCommand("indoor_unlock",v.getContext());
new CommandExecuter(getDoorKey()).doInBackground("indoor_unlock");
Toast.makeText(v.getContext(), getString(R.string.door_unlock), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(v.getContext(), "Konnte Befehl „Tür aufschließen“ nicht ausführen.", Toast.LENGTH_SHORT).show();
}
} }
/** /**
@ -209,20 +206,19 @@ public class MainActivity extends ActionBarActivity {
* @param v * @param v
*/ */
public void buttonUnlockInnerDoor(View v) { public void buttonUnlockInnerDoor(View v) {
try { executeCommand("indoor_open",v.getContext());
new CommandExecuter(getDoorKey()).doInBackground("indoor_open");
Toast.makeText(v.getContext(), getString(R.string.door_open), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(v.getContext(), "Konnte Befehl „Tür öffnen“ nicht ausführen.", Toast.LENGTH_SHORT).show();
}
} }
public void buttonLockInnerDoor(View v) { public void buttonLockInnerDoor(View v) {
try { executeCommand("indoor_lock",v.getContext());
new CommandExecuter(getDoorKey()).doInBackground("indoor_lock");
Toast.makeText(v.getContext(), getString(R.string.door_open), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(v.getContext(), "Konnte Befehl „Tür schließen“ nicht ausführen.", Toast.LENGTH_SHORT).show();
} }
private void executeCommand(String cmd, Context context) {
TrustManager[] trustManagers = new TrustManager[] { new PinningTrustManager(SystemKeyStore.getInstance(context),
new String[] { "F1E2BB0724ACF34E60557DE95BD3DD30BCD08817" }, 0) };
//Ion.getDefault(this).configure().setLogging("iontest", Log.VERBOSE);
Ion ion = Ion.getInstance(context, "tuer");
ion.getHttpClient().getSSLSocketMiddleware().setTrustManagers(trustManagers);
ion.with(this).load("https://tuer.krautspace.de/cgi-bin/kraut.space?secret=" + this.getDoorKey() + "&cmd=" + cmd).asString();
} }
} }

21
tueroeffner.iml Normal file
View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>