diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/DatabaseFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/DatabaseFragment.java index c70f172..81dd4f4 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/DatabaseFragment.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/DatabaseFragment.java @@ -71,6 +71,11 @@ public class DatabaseFragment extends Fragment implements AdapterView.OnItemClic listAdapter = new ArrayAdapter(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); diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java index b3b048c..a33e57b 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/LoginFragment.java @@ -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 diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/TableFragment.java b/app/src/main/java/info/nerull7/mysqlbrowser/TableFragment.java index 1007f2c..c46e42d 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/TableFragment.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/TableFragment.java @@ -70,6 +70,11 @@ public class TableFragment extends Fragment implements AdapterView.OnItemClickLi listAdapter = new ArrayAdapter(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); diff --git a/app/src/main/java/info/nerull7/mysqlbrowser/db/AsyncDatabaseConnector.java b/app/src/main/java/info/nerull7/mysqlbrowser/db/AsyncDatabaseConnector.java index 3aceda8..f8cd69e 100644 --- a/app/src/main/java/info/nerull7/mysqlbrowser/db/AsyncDatabaseConnector.java +++ b/app/src/main/java/info/nerull7/mysqlbrowser/db/AsyncDatabaseConnector.java @@ -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) { Listlist = 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 diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 35ba264..61ab713 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -43,5 +43,6 @@ Zapamiętaj poświadczenia URL Wtyczki php Brak połączenia internetowego + Przekroczono czas połączenia diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c1647e7..7103eb5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,5 +50,6 @@ Connector URL No Internet Connection Connection timeout + Connection reset by server