From 287df0f12a3022eabed0020f6a0ec686135caf4d Mon Sep 17 00:00:00 2001 From: Alexander James Wallar Date: Mon, 20 Jan 2014 00:47:45 -0500 Subject: [PATCH] Added better AsyncRequest Control --- .../src/com/locaudio/api/Locaudio.java | 98 +++---------------- .../src/com/locaudio/net/AsyncGetRequest.java | 48 +++++++++ .../com/locaudio/net/AsyncPostRequest.java | 52 ++++++++++ 3 files changed, 111 insertions(+), 87 deletions(-) create mode 100644 app/Locabean/src/com/locaudio/net/AsyncGetRequest.java create mode 100644 app/Locabean/src/com/locaudio/net/AsyncPostRequest.java diff --git a/app/Locabean/src/com/locaudio/api/Locaudio.java b/app/Locabean/src/com/locaudio/api/Locaudio.java index de25e95..26ed76f 100644 --- a/app/Locabean/src/com/locaudio/api/Locaudio.java +++ b/app/Locabean/src/com/locaudio/api/Locaudio.java @@ -1,13 +1,10 @@ package com.locaudio.api; import java.io.IOException; -import java.util.concurrent.ExecutionException; import org.apache.http.client.ClientProtocolException; -import android.os.AsyncTask; - -import com.locaudio.net.Requests; +import com.locaudio.net.*; public class Locaudio extends Requests { private static final String NAMES_ROUTE = "names"; @@ -19,95 +16,22 @@ public Locaudio(String ipAddress, int port) { } public Location[] getSoundLocations(final String soundName) { - - AsyncTask task = new AsyncTask() { - - @Override - protected Location[] doInBackground(Requests... reqs) { - try { - return reqs[0].get(Location[].class, LOCATIONS_ROUTE, - soundName); - } catch (ClientProtocolException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - }; - - try { - task.execute(this); - return task.get(); - } catch (InterruptedException e) { - e.printStackTrace(); - return null; - } catch (ExecutionException e) { - e.printStackTrace(); - return null; - } + AsyncGetRequest agr = new AsyncGetRequest( + Location[].class, LOCATIONS_ROUTE, soundName); + return agr.getResponse(this); } public String[] getNames() throws ClientProtocolException, IOException { - AsyncTask task = new AsyncTask() { - - @Override - protected String[] doInBackground(Requests... reqs) { - try { - return reqs[0].get(String[].class, NAMES_ROUTE); - } catch (ClientProtocolException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - }; - - try { - task.execute(this); - return task.get(); - } catch (InterruptedException e) { - e.printStackTrace(); - return null; - } catch (ExecutionException e) { - e.printStackTrace(); - return null; - } + AsyncGetRequest agr = new AsyncGetRequest( + String[].class, NAMES_ROUTE); + return agr.getResponse(this); } public NotifyResponse notifyEvent(final NotifyForm event) throws ClientProtocolException, IOException { - AsyncTask task = new AsyncTask() { - - @Override - protected NotifyResponse doInBackground(Requests... reqs) { - try { - return reqs[0].post(NotifyResponse.class, event.toMap(), NOTIFY_ROUTE); - } catch (ClientProtocolException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - }; - - try { - task.execute(this); - return task.get(); - } catch (InterruptedException e) { - e.printStackTrace(); - return null; - } catch (ExecutionException e) { - e.printStackTrace(); - return null; - } + AsyncPostRequest apr = new AsyncPostRequest( + NotifyResponse.class, event.toMap(), NOTIFY_ROUTE); + + return apr.getResponse(this); } } diff --git a/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java b/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java new file mode 100644 index 0000000..cdaa03f --- /dev/null +++ b/app/Locabean/src/com/locaudio/net/AsyncGetRequest.java @@ -0,0 +1,48 @@ +package com.locaudio.net; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +import org.apache.http.client.ClientProtocolException; + +import android.os.AsyncTask; + +import com.locaudio.net.Requests; + +public class AsyncGetRequest extends AsyncTask { + + private Class tClass; + private String[] urlParams = null; + + public AsyncGetRequest(Class tClass, String... urlParams) { + this.tClass = tClass; + this.urlParams = urlParams; + } + + public T getResponse(Requests reqs) { + try { + this.execute(reqs); + return this.get(); + } catch (InterruptedException e) { + e.printStackTrace(); + return null; + } catch (ExecutionException e) { + e.printStackTrace(); + return null; + } + } + + @Override + protected T doInBackground(Requests... reqs) { + try { + return reqs[0].get(this.tClass, this.urlParams); + } catch (ClientProtocolException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java b/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java new file mode 100644 index 0000000..f51fa1d --- /dev/null +++ b/app/Locabean/src/com/locaudio/net/AsyncPostRequest.java @@ -0,0 +1,52 @@ +package com.locaudio.net; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import org.apache.http.client.ClientProtocolException; + +import android.os.AsyncTask; + +import com.locaudio.net.Requests; + +public class AsyncPostRequest extends AsyncTask { + + private Class tClass; + private Map postForm = null; + private String[] urlParams = null; + + public AsyncPostRequest(Class tClass, Map postForm, + String... urlParams) { + this.tClass = tClass; + this.postForm = postForm; + this.urlParams = urlParams; + } + + public T getResponse(Requests reqs) { + try { + this.execute(reqs); + return this.get(); + } catch (InterruptedException e) { + e.printStackTrace(); + return null; + } catch (ExecutionException e) { + e.printStackTrace(); + return null; + } + } + + @Override + protected T doInBackground(Requests... reqs) { + try { + return reqs[0].post(this.tClass, this.postForm, this.urlParams); + } catch (ClientProtocolException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + +}