Skip to content

Commit

Permalink
Added better AsyncRequest Control
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander James Wallar committed Jan 20, 2014
1 parent 4080336 commit 287df0f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 87 deletions.
98 changes: 11 additions & 87 deletions app/Locabean/src/com/locaudio/api/Locaudio.java
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -19,95 +16,22 @@ public Locaudio(String ipAddress, int port) {
}

public Location[] getSoundLocations(final String soundName) {

AsyncTask<Requests, Integer, Location[]> task = new AsyncTask<Requests, Integer, Location[]>() {

@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<Location[]> agr = new AsyncGetRequest<Location[]>(
Location[].class, LOCATIONS_ROUTE, soundName);
return agr.getResponse(this);
}

public String[] getNames() throws ClientProtocolException, IOException {
AsyncTask<Requests, Integer, String[]> task = new AsyncTask<Requests, Integer, String[]>() {

@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<String[]> agr = new AsyncGetRequest<String[]>(
String[].class, NAMES_ROUTE);
return agr.getResponse(this);
}

public NotifyResponse notifyEvent(final NotifyForm event)
throws ClientProtocolException, IOException {
AsyncTask<Requests, Integer, NotifyResponse> task = new AsyncTask<Requests, Integer, NotifyResponse>() {

@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<NotifyResponse> apr = new AsyncPostRequest<NotifyResponse>(
NotifyResponse.class, event.toMap(), NOTIFY_ROUTE);

return apr.getResponse(this);
}
}
48 changes: 48 additions & 0 deletions app/Locabean/src/com/locaudio/net/AsyncGetRequest.java
Original file line number Diff line number Diff line change
@@ -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<T> extends AsyncTask<Requests, Integer, T> {

private Class<T> tClass;
private String[] urlParams = null;

public AsyncGetRequest(Class<T> 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;
}
}

}
52 changes: 52 additions & 0 deletions app/Locabean/src/com/locaudio/net/AsyncPostRequest.java
Original file line number Diff line number Diff line change
@@ -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<T> extends AsyncTask<Requests, Integer, T> {

private Class<T> tClass;
private Map<String, ?> postForm = null;
private String[] urlParams = null;

public AsyncPostRequest(Class<T> tClass, Map<String, ?> 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;
}
}

}

0 comments on commit 287df0f

Please sign in to comment.