Handling errors DO NOT MERGE

This commit is contained in:
Przemek Grondek 2014-08-20 23:29:44 +02:00
parent f223b58e21
commit 414cb10630
6 changed files with 60 additions and 33 deletions

View file

@ -71,6 +71,11 @@ public class DatabaseFragment extends Fragment implements AdapterView.OnItemClic
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, databases);
databasesListView.setAdapter(listAdapter);
databasesListView.setOnItemClickListener(this);
} else if(Static.asyncDatabaseConnector.hadError) {
if(Static.asyncDatabaseConnector.errorMsg!=null)
Static.showErrorAlert(Static.asyncDatabaseConnector.errorMsg, getActivity());
else
Static.showErrorAlert(getResources().getString(Static.asyncDatabaseConnector.errorMsgResource), getActivity());
} else {
TextView errorMessage = new TextView(getActivity());
errorMessage.setText(R.string.error_no_databases);

View file

@ -112,7 +112,12 @@ public class LoginFragment extends Fragment implements View.OnClickListener, Asy
startActivity(intent);
}
else {
Static.showErrorAlert(AsyncDatabaseConnector.errorMsg, getActivity());
if(AsyncDatabaseConnector.errorMsg!=null)
Static.showErrorAlert(AsyncDatabaseConnector.errorMsg, getActivity());
else {
String errorMsg = getResources().getString(AsyncDatabaseConnector.errorMsgResource);
Static.showErrorAlert(errorMsg, getActivity());
}
}
loginButton.setEnabled(true); // Now we can click button again

View file

@ -70,6 +70,11 @@ public class TableFragment extends Fragment implements AdapterView.OnItemClickLi
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, tables);
tablesList.setAdapter(listAdapter);
tablesList.setOnItemClickListener(this);
} else if(Static.asyncDatabaseConnector.hadError) {
if (Static.asyncDatabaseConnector.errorMsg != null)
Static.showErrorAlert(Static.asyncDatabaseConnector.errorMsg, getActivity());
else
Static.showErrorAlert(getResources().getString(Static.asyncDatabaseConnector.errorMsgResource), getActivity());
} else {
TextView errorMessage = new TextView(getActivity());
errorMessage.setText(R.string.error_no_tables);

View file

@ -1,8 +1,6 @@
package info.nerull7.mysqlbrowser.db;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import org.json.JSONArray;
import org.json.JSONException;
@ -12,8 +10,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
@ -49,6 +48,8 @@ public class AsyncDatabaseConnector {
private MatrixReturnListener matrixReturnListener;
public static String errorMsg;
public static int errorMsgResource;
public static boolean hadError;
private OnPostExecuteListener onPostExecuteListener;
public AsyncDatabaseConnector(String login, String password, String url){
@ -56,6 +57,8 @@ public class AsyncDatabaseConnector {
this.password = password;
this.url = url;
hadError = false;
booleanReturnListener=null;
stringReturnListener=null;
listReturnListener=null;
@ -101,6 +104,8 @@ public class AsyncDatabaseConnector {
@Override
public void onFinished(String data, String error) {
List<String>list = null;
if(data==null)
return;
try {
list = parseListFromJSON(data);
} catch (JSONException e) { e.printStackTrace(); }
@ -393,38 +398,42 @@ public class AsyncDatabaseConnector {
}
private String httpRequest(String urlRequest) throws IOException {
URL url = new URL(urlRequest);
InputStream inputStream = null;
String response;
// Log.d("URL REQUEST", urlRequest);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // TODO Handling no connection
urlConnection.setReadTimeout(READ_TIMEOUT);
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
urlConnection.setRequestMethod(CONNECTION_REQUEST_METHOD);
try {
urlConnection.connect();
} catch (ConnectException e) {
// errorMsg = e.getMessage();
if (e.getCause().equals(CONNECTION_REQUEST_METHOD)) {
errorMsg == R.string.error_connection_timeout; // FIXME
}
}
URL url = new URL(urlRequest);
InputStream inputStream = null;
String response;
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try {
inputStream = urlConnection.getInputStream();
response = readStream(inputStream);
} finally {
if(inputStream!=null)
inputStream.close();
urlConnection.disconnect();
// Log.d("URL REQUEST", urlRequest);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(READ_TIMEOUT);
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
urlConnection.setRequestMethod(CONNECTION_REQUEST_METHOD);
urlConnection.connect();
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try {
inputStream = urlConnection.getInputStream();
response = readStream(inputStream);
} finally {
if (inputStream != null)
inputStream.close();
urlConnection.disconnect();
}
return response;
} else {
errorString = "ERROR " + urlConnection.getResponseCode() + ": " + urlConnection.getResponseMessage();
return null;
}
return response;
}
else {
errorString = "ERROR "+urlConnection.getResponseCode()+": "+urlConnection.getResponseMessage();
} catch (SocketException exception){
hadError = true;
errorMsgResource = R.string.error_connection_reset;
errorMsg = null;
return null;
} catch (SocketTimeoutException exception){
hadError = true;
errorMsgResource = R.string.error_connection_timeout;
errorMsg = null;
return null;
}
}
@ -448,6 +457,7 @@ public class AsyncDatabaseConnector {
@Override
protected String doInBackground(String... strings) {
hadError = false;
try {
String data = httpRequest(strings[0]);
onFinishedListener.onFinished(data, errorMsg); // Can't be null cos we demand listener in constructor

View file

@ -43,5 +43,6 @@
<string name="save_credentials">Zapamiętaj poświadczenia</string>
<string name="connector_url">URL Wtyczki php</string>
<string name="no_connection">Brak połączenia internetowego</string>
<string name="error_connection_timeout">Przekroczono czas połączenia</string>
</resources>

View file

@ -50,5 +50,6 @@
<string name="connector_url">Connector URL</string>
<string name="no_connection">No Internet Connection</string>
<string name="error_connection_timeout">Connection timeout</string>
<string name="error_connection_reset">Connection reset by server</string>
</resources>