Time to POST it

Moved away from sending data in GET request and now using POST requests.
This commit is contained in:
Przemek Grondek 2014-09-16 11:25:54 +02:00
parent d478b474fb
commit fae910fc8c

View file

@ -2,8 +2,6 @@ package info.nerull7.mysqlbrowser.db;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -12,8 +10,9 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@ -68,16 +67,17 @@ public class AsyncDatabaseConnector {
matrixReturnListener=null; matrixReturnListener=null;
} }
private String actionUrlBuilder(String action){ private Request requestBuilder(String action){
String urlBuilder = url; Request request = new Request(url);
urlBuilder += "?u="+login; String urlData = "u="+login
urlBuilder += "&p="+password; + "&p="+password
urlBuilder += "&a="+action; + "&a="+action;
request.data = urlData;
return urlBuilder; return request;
} }
private String actionUrlBuilder(String action, String argument, String value){ private Request actionUrlBuilder(String action, String argument, String value){
ArrayList<String> arguments = new ArrayList<String>(); ArrayList<String> arguments = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>(); ArrayList<String> values = new ArrayList<String>();
arguments.add(argument); arguments.add(argument);
@ -85,11 +85,11 @@ public class AsyncDatabaseConnector {
return this.actionUrlBuilder(action, arguments, values); return this.actionUrlBuilder(action, arguments, values);
} }
private String actionUrlBuilder(String action, List<String> arguments, List<String> values){ // TODO Better UrlBuilder this is shit only for use private Request actionUrlBuilder(String action, List<String> arguments, List<String> values){ // TODO Better UrlBuilder this is shit only for use
String urlBuilder = actionUrlBuilder(action); Request urlBuilder = requestBuilder(action);
for (int i = 0; i < arguments.size(); i++) { for (int i = 0; i < arguments.size(); i++) {
try { try {
urlBuilder += "&" + arguments.get(i) + "=" + URLEncoder.encode(values.get(i), "UTF-8"); urlBuilder.data += "&" + arguments.get(i) + "=" + URLEncoder.encode(values.get(i), "UTF-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -102,7 +102,7 @@ public class AsyncDatabaseConnector {
this.database = database; this.database = database;
} }
private void getList(String urlQuery){ private void getList(Request urlQuery){
Downloader downloader = new Downloader(new Downloader.OnFinishedListener() { Downloader downloader = new Downloader(new Downloader.OnFinishedListener() {
@Override @Override
public void onFinished(String data, String error) { public void onFinished(String data, String error) {
@ -119,7 +119,7 @@ public class AsyncDatabaseConnector {
downloader.execute(urlQuery); downloader.execute(urlQuery);
} }
private void getMatrix(String urlQuery){ private void getMatrix(Request urlQuery){
Downloader downloader = new Downloader(new Downloader.OnFinishedListener() { Downloader downloader = new Downloader(new Downloader.OnFinishedListener() {
@Override @Override
public void onFinished(String data, String error) { public void onFinished(String data, String error) {
@ -175,12 +175,12 @@ public class AsyncDatabaseConnector {
booleanReturnListener.onBooleanReturn(listenerData); booleanReturnListener.onBooleanReturn(listenerData);
} }
}, onPostExecuteListener, resources); }, onPostExecuteListener, resources);
downloader.execute(actionUrlBuilder(ACTION_LOGIN)); downloader.execute(requestBuilder(ACTION_LOGIN));
return false; return false;
} }
public void getDatabases(){ public void getDatabases(){
getList(actionUrlBuilder(ACTION_DATABASE_LIST)); getList(requestBuilder(ACTION_DATABASE_LIST));
} }
public void getTables(){ public void getTables(){
@ -230,7 +230,7 @@ public class AsyncDatabaseConnector {
args.add("t"); args.add("t");
values.add(table); values.add(table);
String urlQuery = actionUrlBuilder(ACTION_ENTRIES_COUNT, args, values); Request urlQuery = actionUrlBuilder(ACTION_ENTRIES_COUNT, args, values);
Downloader downloader = new Downloader(new Downloader.OnFinishedListener() { Downloader downloader = new Downloader(new Downloader.OnFinishedListener() {
@Override @Override
public void onFinished(String data, String error) { public void onFinished(String data, String error) {
@ -249,7 +249,7 @@ public class AsyncDatabaseConnector {
public void updateElement(String table, List<String> header, List<String> oldValues, List<String> newValues){ public void updateElement(String table, List<String> header, List<String> oldValues, List<String> newValues){
JSONArray headerJSON = new JSONArray(); JSONArray headerJSON = new JSONArray();
JSONArray newValuesJSON = new JSONArray(); JSONArray newValuesJSON = new JSONArray();
String request; Request request;
ArrayList<String> args = new ArrayList<String>(); ArrayList<String> args = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>(); ArrayList<String> values = new ArrayList<String>();
@ -300,7 +300,7 @@ public class AsyncDatabaseConnector {
public void removeElement(String table, List<String> header, List<String> values) { public void removeElement(String table, List<String> header, List<String> values) {
JSONArray headerJSON = new JSONArray(); JSONArray headerJSON = new JSONArray();
JSONArray valuesJSON = new JSONArray(); JSONArray valuesJSON = new JSONArray();
String request; Request request;
ArrayList<String> args = new ArrayList<String>(); ArrayList<String> args = new ArrayList<String>();
ArrayList<String> argValues = new ArrayList<String>(); ArrayList<String> argValues = new ArrayList<String>();
@ -387,13 +387,13 @@ public class AsyncDatabaseConnector {
void onPostExecute(); void onPostExecute();
} }
private static class Downloader extends AsyncTask<String, Void, String> { private static class Downloader extends AsyncTask<Request, Void, String> {
private OnFinishedListener onFinishedListener; private OnFinishedListener onFinishedListener;
private OnPostExecuteListener onPostExecuteListener; private OnPostExecuteListener onPostExecuteListener;
private String errorString; private String errorString;
private Resources resources; private Resources resources;
public static final String CONNECTION_REQUEST_METHOD = "GET"; public static final String CONNECTION_REQUEST_METHOD = "POST";
public static final int CONNECTION_TIMEOUT = 15000; public static final int CONNECTION_TIMEOUT = 15000;
public static final int READ_TIMEOUT = 10000; public static final int READ_TIMEOUT = 10000;
@ -404,8 +404,8 @@ public class AsyncDatabaseConnector {
errorString = null; errorString = null;
} }
private String httpRequest(String urlRequest) throws IOException { private String httpRequest(Request urlRequest) throws IOException {
URL url = new URL(urlRequest); URL url = new URL(urlRequest.url);
InputStream inputStream = null; InputStream inputStream = null;
String response; String response;
@ -416,6 +416,13 @@ public class AsyncDatabaseConnector {
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
urlConnection.setRequestMethod(CONNECTION_REQUEST_METHOD); urlConnection.setRequestMethod(CONNECTION_REQUEST_METHOD);
OutputStream outputStream = urlConnection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(urlRequest.data);
outputStreamWriter.flush();
outputStreamWriter.close();
outputStream.close();
try { try {
urlConnection.connect(); urlConnection.connect();
@ -472,9 +479,9 @@ public class AsyncDatabaseConnector {
} }
@Override @Override
protected String doInBackground(String... strings) { protected String doInBackground(Request... requests) {
try { try {
String data = httpRequest(strings[0]); String data = httpRequest(requests[0]);
onFinishedListener.onFinished(data, errorString); // Can't be null cos we demand listener in constructor onFinishedListener.onFinished(data, errorString); // Can't be null cos we demand listener in constructor
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -497,4 +504,13 @@ public class AsyncDatabaseConnector {
} }
} }
class Request{
String url;
String data;
Request(String url){
this.url = url;
}
}
} }