Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
Better error handling for network connections.
Browse files Browse the repository at this point in the history
Add status constants to OnlineHelper.
Add failure messages to strings.xml.
Fix URL for auth token request.
  • Loading branch information
ashtom committed Jul 30, 2012
1 parent 5d831fe commit 1edfa7a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
3 changes: 3 additions & 0 deletions gen/de/codenauts/hockeyapp/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public static final class string {
public static final int menu_logout=0x7f08000e;
public static final int menu_refresh=0x7f08000f;
public static final int menu_update=0x7f080010;
public static final int status_login_failure=0x7f080011;
public static final int status_network_failure=0x7f080012;
public static final int status_unknown_failure=0x7f080013;
}
public static final class style {
public static final int ActionBar_HockeyApp=0x7f090007;
Expand Down
3 changes: 3 additions & 0 deletions gen/net/hockeyapp/android/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public static final class string {
public static final int menu_logout=0x7f08000e;
public static final int menu_refresh=0x7f08000f;
public static final int menu_update=0x7f080010;
public static final int status_login_failure=0x7f080011;
public static final int status_network_failure=0x7f080012;
public static final int status_unknown_failure=0x7f080013;
}
public static final class style {
public static final int ActionBar_HockeyApp=0x7f090007;
Expand Down
4 changes: 4 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
<string name="menu_logout">Logout</string>
<string name="menu_refresh">Refresh</string>
<string name="menu_update">Check for Updates</string>

<string name="status_login_failure">Login failed. Please check your credentials and try again.</string>
<string name="status_network_failure">Connection failed. Please check your network settings and try again.</string>
<string name="status_unknown_failure">Unknown error. Please try again or contact support.</string>
</resources>
13 changes: 9 additions & 4 deletions src/main/java/de/codenauts/hockeyapp/AppsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class AppsTask extends AsyncTask<String, String, JSONArray> {
private boolean finished;
private int status = OnlineHelper.STATUS_UNKNOWN_ERROR;
private JSONArray apps;
private MainActivity activity;
private String token;
Expand Down Expand Up @@ -39,15 +40,17 @@ public void detach() {
@Override
protected JSONArray doInBackground(String... params) {
try {
return getTokens();
return getApps();
}
catch (IOException e) {
status = OnlineHelper.STATUS_NETWORK_ERROR;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

private JSONArray getTokens() throws IOException, JSONException {
private JSONArray getApps() throws IOException, JSONException {
URL url = new URL(OnlineHelper.BASE_URL + OnlineHelper.APPS_ACTION);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

Expand All @@ -59,6 +62,7 @@ private JSONArray getTokens() throws IOException, JSONException {
return parseJSONFromString(jsonString);
}
else {
status = OnlineHelper.STATUS_LOGIN_ERROR;
return null;
}
}
Expand All @@ -69,6 +73,7 @@ private JSONArray parseJSONFromString(String jsonString) throws JSONException {
return (JSONArray)json.get("apps");
}
else {
status = OnlineHelper.STATUS_LOGIN_ERROR;
return null;
}
}
Expand All @@ -91,7 +96,7 @@ protected void onPostExecute(JSONArray apps) {

private void handleResult() {
if (this.apps == null) {
activity.didFailToReceiveApps();
activity.didFailToReceiveApps(status);
}
else {
activity.didReceiveApps(this.apps);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/codenauts/hockeyapp/LoginTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public class LoginTask extends AsyncTask<String, String, String> {
private boolean finished;
private int status = OnlineHelper.STATUS_UNKNOWN_ERROR;
private MainActivity activity;
private String credentials;
private String token;
Expand Down Expand Up @@ -43,6 +44,9 @@ protected String doInBackground(String... params) {
JSONArray tokens = getTokens();
return findToken(tokens, true);
}
catch (IOException e) {
status = OnlineHelper.STATUS_NETWORK_ERROR;
}
catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -106,6 +110,7 @@ private JSONArray getTokens() throws IOException, JSONException {
return parseJSONFromString(jsonString);
}
else {
status = OnlineHelper.STATUS_LOGIN_ERROR;
return null;
}
}
Expand All @@ -116,6 +121,7 @@ private JSONArray parseJSONFromString(String jsonString) throws JSONException {
return (JSONArray)json.get("tokens");
}
else {
status = OnlineHelper.STATUS_LOGIN_ERROR;
return null;
}
}
Expand All @@ -139,7 +145,7 @@ protected void onPostExecute(String token) {

private void handleResult() {
if (this.token == null) {
activity.loginFailed();
activity.loginFailed(status);
}
else {
activity.loginWasSuccesful(this.token);
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/de/codenauts/hockeyapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem refreshItem = menu.getItem(1);
MenuItem logoutItem = menu.getItem(2);
MenuItem refreshItem = menu.getItem(2);
MenuItem logoutItem = menu.getItem(3);

if (getAPIToken() == null) {
refreshItem.setEnabled(false);
Expand Down Expand Up @@ -331,15 +331,31 @@ public void loginWasSuccesful(String token) {
}

@SuppressWarnings("deprecation")
public void loginFailed() {
public void loginFailed(int status) {
loginTask = null;
Toast.makeText(this, R.string.login_view_failed_toast, Toast.LENGTH_LONG).show();
Toast.makeText(this, getMessageForStatus(status), Toast.LENGTH_LONG).show();
showDialog(DIALOG_LOGIN);
setStatus(getResources().getString(R.string.main_view_signed_out_label));
}

public void didFailToReceiveApps() {
setStatus("Connection failed. Please try again or check your credentials.");
public void didFailToReceiveApps(int status) {
setStatus(getMessageForStatus(status));
}

private String getMessageForStatus(int status) {
String message = null;
switch (status) {
case OnlineHelper.STATUS_LOGIN_ERROR:
message = getResources().getString(R.string.status_login_failure);
break;
case OnlineHelper.STATUS_NETWORK_ERROR:
message = getResources().getString(R.string.status_network_failure);
break;
default:
message = getResources().getString(R.string.status_unknown_failure);
break;
}
return message;
}

@SuppressWarnings("unchecked")
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/de/codenauts/hockeyapp/OnlineHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import java.net.HttpURLConnection;

public class OnlineHelper {
final static public int STATUS_NO_ERROR = 0;
final static public int STATUS_LOGIN_ERROR = 1;
final static public int STATUS_NETWORK_ERROR = 2;
final static public int STATUS_UNKNOWN_ERROR = 3;

final static public String BASE_URL = "https://rink.hockeyapp.net/api/2/";
final static public String AUTH_ACTION = "auth_tokens";
final static public String AUTH_ACTION = "auth_tokens?format=json";
final static public String APPS_ACTION = "apps?format=json";

public static String getStringFromConnection(HttpURLConnection connection) throws IOException {
Expand Down

0 comments on commit 1edfa7a

Please sign in to comment.