I have refactored checkLogin()
which lives in the LoginActivity
class, but I still think that it can be refactored even better.
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
LoginRequest loginRequest = new LoginRequest(Request.Method.POST, AppConfig.getUrlLogin(), ReqSuccessListener(), ReqErrorListener()) {
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(loginRequest, tag_string_req);
}
The implementation of ReqSuccessListener()
and ReqErrorListener()
also lives in the LoginActivity
class. Which looks like this:
private Response.Listener<String> ReqSuccessListener() {
return new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
session.setLogin(true);
JSONObject jObj = new JSONObject(response);
JSONObject user = jObj.getJSONObject("user");
String uid = user.getString("id");
String name = user.getString("name");
String email = user.getString("email");
// Inserting row in users table
db.addUser(name, email, uid);
// Launch main activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} catch (JSONException e) {
// JSON error
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
}
private Response.ErrorListener ReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
int statusCode = error.networkResponse.statusCode;
NetworkResponse response = error.networkResponse;
Log.d("testerror", "" + statusCode + " " + new String(response.data));
if (statusCode != 200) {
Toast.makeText(getApplicationContext(), new String(response.data), Toast.LENGTH_LONG).show();
hideDialog();
}
}
};
}
My question is simple how can I refactor this even better? Or did I already refactored it good enough?
Also here is my link to show you how the code looks before and after I had refactored it. Here is the link if you want to see it: http://ift.tt/1NMQkCK
So on the left side is the code before the refactoring and on the right side is the code after the refactoring.
Aucun commentaire:
Enregistrer un commentaire