Skip to content

Commit 213ab39

Browse files
committed
Handle errors
1 parent fd40cbf commit 213ab39

8 files changed

Lines changed: 61 additions & 159 deletions

File tree

ExampleApp/src/com/udinic/accounts_example/Main1.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,11 @@ public void run() {
170170
Bundle bnd = future.getResult();
171171

172172
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
173+
mAccountManager.invalidateAuthToken(account.type, authtoken);
173174
runOnUiThread(new Runnable() {
174175
@Override
175176
public void run() {
176-
//To change body of implemented methods use File | Settings | File Templates.
177-
mAccountManager.invalidateAuthToken(account.type, authtoken);
178-
179-
Toast.makeText(getBaseContext(), "dsd", Toast.LENGTH_SHORT).show();
177+
Toast.makeText(getBaseContext(), account.name + " invalidated", Toast.LENGTH_SHORT).show();
180178
}
181179
});
182180
Log.d("udini", "GetToken Bundle is " + bnd);

res/layout/act_login.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
android:layout_height="wrap_content"
1414
android:layout_marginTop="20dp"
1515
android:hint="Email address"
16-
android:text="uuu2@ggg.com"
1716
android:inputType="textEmailAddress"
1817
android:padding="10dp"
1918
/>
@@ -24,15 +23,14 @@
2423
android:layout_marginTop="10dp"
2524
android:inputType="textPassword"
2625
android:hint="Password"
27-
android:text="qwerty"
2826
android:padding="10dp"
2927
/>
3028

3129
<Button android:id="@+id/submit"
3230
android:layout_width="match_parent"
3331
android:layout_height="wrap_content"
3432
android:layout_marginTop="10dp"
35-
android:text="connect"
33+
android:text="Sign in"
3634
android:padding="10dp"/>
3735

3836
<TextView android:id="@+id/signUp"

src/com/udinic/accounts_authenticator_example/authentication/AuthenticatorActivity.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity {
2121
public final static String ARG_ACCOUNT_NAME = "ACCOUNT_NAME";
2222
public final static String ARG_IS_ADDING_NEW_ACCOUNT = "IS_ADDING_ACCOUNT";
2323

24+
public static final String KEY_ERROR_MESSAGE = "ERR_MSG";
25+
2426
public final static String PARAM_USER_PASS = "USER_PASS";
2527

2628
private final int REQ_SIGNUP = 1;
@@ -86,28 +88,31 @@ protected Intent doInBackground(String... params) {
8688

8789
Log.d("udini", TAG + "> Started authenticating");
8890

89-
String authtoken = sServerAuthenticate.userSignIn(userName, userPass, mAuthTokenType);
91+
String authtoken = null;
92+
Bundle data = new Bundle();
93+
try {
94+
authtoken = sServerAuthenticate.userSignIn(userName, userPass, mAuthTokenType);
9095

91-
if (authtoken == null)
92-
return null;
93-
else {
94-
final Intent res = new Intent();
95-
res.putExtra(AccountManager.KEY_ACCOUNT_NAME, userName);
96-
res.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
97-
res.putExtra(AccountManager.KEY_AUTHTOKEN, authtoken);
98-
res.putExtra(PARAM_USER_PASS, userPass);
96+
data.putString(AccountManager.KEY_ACCOUNT_NAME, userName);
97+
data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
98+
data.putString(AccountManager.KEY_AUTHTOKEN, authtoken);
99+
data.putString(PARAM_USER_PASS, userPass);
99100

100-
return res;
101+
} catch (Exception e) {
102+
data.putString(KEY_ERROR_MESSAGE, e.getMessage());
101103
}
104+
105+
final Intent res = new Intent();
106+
res.putExtras(data);
107+
return res;
102108
}
103109

104110
@Override
105111
protected void onPostExecute(Intent intent) {
106-
if (intent != null) {
107-
finishLogin(intent);
112+
if (intent.hasExtra(KEY_ERROR_MESSAGE)) {
113+
Toast.makeText(getBaseContext(), intent.getStringExtra(KEY_ERROR_MESSAGE), Toast.LENGTH_SHORT).show();
108114
} else {
109-
Toast.makeText(getBaseContext(), "Wrong credentials", Toast.LENGTH_SHORT).show();
110-
finish();
115+
finishLogin(intent);
111116
}
112117
}
113118
}.execute();

src/com/udinic/accounts_authenticator_example/authentication/ParseComServerAuthenticate.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public String userSignUp(String name, String email, String pass, String authType
4242
String authtoken = null;
4343
try {
4444
HttpResponse response = httpClient.execute(httpPost);
45+
String responseString = EntityUtils.toString(response.getEntity());
4546

46-
if (response.getStatusLine().getStatusCode() != 201)
47-
throw new Exception("User not created");
47+
if (response.getStatusLine().getStatusCode() != 201) {
48+
ParseComError error = new Gson().fromJson(responseString, ParseComError.class);
49+
throw new Exception("Error creating user["+error.code+"] - " + error.error);
50+
}
4851

49-
String responseString = EntityUtils.toString(response.getEntity());
5052

5153
User createdUser = new Gson().fromJson(responseString, User.class);
5254

@@ -60,7 +62,7 @@ public String userSignUp(String name, String email, String pass, String authType
6062
}
6163

6264
@Override
63-
public String userSignIn(String user, String pass, String authType) {
65+
public String userSignIn(String user, String pass, String authType) throws Exception {
6466

6567
Log.d("udini", "userSignIn");
6668

@@ -74,14 +76,8 @@ public String userSignIn(String user, String pass, String authType) {
7476
} catch (UnsupportedEncodingException e) {
7577
e.printStackTrace();
7678
}
77-
// String queryEnc = URLEncodedUtils.format(query, "utf-8");
7879
url += "?" + query;
7980

80-
// try {
81-
// url = ;
82-
// } catch (UnsupportedEncodingException e) {
83-
// e.printStackTrace();
84-
// }
8581
HttpGet httpGet = new HttpGet(url);
8682

8783
httpGet.addHeader("X-Parse-Application-Id", "XUafJTkPikD5XN5HxciweVuSe12gDgk2tzMltOhr");
@@ -97,19 +93,26 @@ public String userSignIn(String user, String pass, String authType) {
9793
try {
9894
HttpResponse response = httpClient.execute(httpGet);
9995

100-
if (response.getStatusLine().getStatusCode() == 200) {
101-
String responseString = EntityUtils.toString(response.getEntity());
102-
User loggedUser = new Gson().fromJson(responseString, User.class);
103-
authtoken = loggedUser.sessionToken;
96+
String responseString = EntityUtils.toString(response.getEntity());
97+
if (response.getStatusLine().getStatusCode() != 200) {
98+
ParseComError error = new Gson().fromJson(responseString, ParseComError.class);
99+
throw new Exception("Error signing-in ["+error.code+"] - " + error.error);
104100
}
105101

102+
User loggedUser = new Gson().fromJson(responseString, User.class);
103+
authtoken = loggedUser.sessionToken;
104+
106105
} catch (IOException e) {
107106
e.printStackTrace();
108107
}
109108

110109
return authtoken;
111110
}
112111

112+
private class ParseComError implements Serializable {
113+
int code;
114+
String error;
115+
}
113116
private class User implements Serializable {
114117

115118
private String firstName;

src/com/udinic/accounts_authenticator_example/authentication/ServerAuthenticate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
*/
88
public interface ServerAuthenticate {
99
public String userSignUp(final String name, final String email, final String pass, String authType) throws Exception;
10-
public String userSignIn(final String user, final String pass, String authType);
10+
public String userSignIn(final String user, final String pass, String authType) throws Exception;
1111
}

src/com/udinic/accounts_authenticator_example/authentication/SignUpActivity.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import android.util.Log;
99
import android.view.View;
1010
import android.widget.TextView;
11+
import android.widget.Toast;
1112
import com.udinic.accounts_authenticator_example.R;
1213

1314
import static com.udinic.accounts_authenticator_example.authentication.AccountGeneral.sServerAuthenticate;
1415
import static com.udinic.accounts_authenticator_example.authentication.AuthenticatorActivity.ARG_ACCOUNT_TYPE;
16+
import static com.udinic.accounts_authenticator_example.authentication.AuthenticatorActivity.KEY_ERROR_MESSAGE;
1517
import static com.udinic.accounts_authenticator_example.authentication.AuthenticatorActivity.PARAM_USER_PASS;
1618

1719
/**
@@ -58,25 +60,31 @@ protected Intent doInBackground(String... params) {
5860
Log.d("udini", TAG + "> Started authenticating");
5961

6062
String authtoken = null;
63+
Bundle data = new Bundle();
6164
try {
6265
authtoken = sServerAuthenticate.userSignUp(name, accountName, accountPassword, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS);
66+
67+
data.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
68+
data.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType);
69+
data.putString(AccountManager.KEY_AUTHTOKEN, authtoken);
70+
data.putString(PARAM_USER_PASS, accountPassword);
6371
} catch (Exception e) {
64-
e.printStackTrace();
72+
data.putString(KEY_ERROR_MESSAGE, e.getMessage());
6573
}
6674

6775
final Intent res = new Intent();
68-
res.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName);
69-
res.putExtra(AccountManager.KEY_ACCOUNT_TYPE, mAccountType);
70-
res.putExtra(AccountManager.KEY_AUTHTOKEN, authtoken);
71-
res.putExtra(PARAM_USER_PASS, accountPassword);
72-
76+
res.putExtras(data);
7377
return res;
7478
}
7579

7680
@Override
7781
protected void onPostExecute(Intent intent) {
78-
setResult(RESULT_OK, intent);
79-
finish();
82+
if (intent.hasExtra(KEY_ERROR_MESSAGE)) {
83+
Toast.makeText(getBaseContext(), intent.getStringExtra(KEY_ERROR_MESSAGE), Toast.LENGTH_SHORT).show();
84+
} else {
85+
setResult(RESULT_OK, intent);
86+
finish();
87+
}
8088
}
8189
}.execute();
8290
}

src/com/udinic/accounts_authenticator_example/authentication/UdinicAuthenticator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account accoun
6868
if (TextUtils.isEmpty(authToken)) {
6969
final String password = am.getPassword(account);
7070
if (password != null) {
71-
authToken = sServerAuthenticate.userSignIn(account.name, password, authTokenType);
71+
try {
72+
authToken = sServerAuthenticate.userSignIn(account.name, password, authTokenType);
73+
} catch (Exception e) {
74+
e.printStackTrace();
75+
}
7276
}
7377
}
7478

src/com/udinic/accounts_authenticator_example/authentication/UdinicServerAuthenticate.java

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)