Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends BaseActivity implements VolleyTasksListener {
- private EditText edtUser;
- private EditText edtPassword;
- private Button btnLogin;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- findViews();
- }
- private void callLoginService() {
- if (ConnectionUtils.isConnectedNetwork(this)) {
- if (!edtUser.getText().toString().equals("")) {
- if (!edtPassword.getText().toString().equals("")) {
- JSONObject jsonMainObject = new JSONObject();
- try {
- jsonMainObject.put("username", edtUser.getText().toString());
- jsonMainObject.put("password", edtPassword.getText().toString());
- VolleyTasks.makeVolleyPost(LoginActivity.this, Constant.LOGIN_URL, jsonMainObject, "Login");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else {
- DialogUtils.showPositiveDialog(this, "", "Please enter valid Password");
- }
- } else {
- DialogUtils.showPositiveDialog(this, "", "Please enter valid UserId ");
- }
- } else {
- DialogUtils.showPositiveDialog(this, "", getString(R.string.error_connection));
- }
- }
- private void findViews() {
- edtUser = (EditText) findViewById(R.id.edt_user_id);
- edtPassword = (EditText) findViewById(R.id.edt_password);
- btnLogin = (Button) findViewById(R.id.btn_login);
- btnLogin.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- super.onClick(v);
- switch (v.getId()) {
- case R.id.btn_login:
- callLoginService();
- break;
- }
- }
- @Override
- public void handleError(Exception error) {
- L.d("error" + error);}
- @Override
- public void handleResult(String method_name, JSONObject response) {
- L.d("response" + response);
- if (response != null) {
- GsonBuilder gsonBuilder = new GsonBuilder();
- gsonBuilder.serializeNulls();
- Gson gson = gsonBuilder.create();
- if (method_name.equals("Login")) {
- AbstractLogin abstractLogin = gson.fromJson(response.toString(), AbstractLogin.class);
- if (abstractLogin != null) {
- if (abstractLogin.getApi().equals("UserLogin")) {
- Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
- startActivity(intent);
- finish();
- } else {
- DialogUtils.showPositiveDialog(this, "", getString(R.string.err_msg_request_failed));
- }
- }
- }
- }
- }}
- public class VolleyTasks {
- static VolleyTasksListener volley;
- private static String TAG = VolleyTasks.class.getSimpleName();
- private static Context mContext;
- public static void makeVolleyPost(Fragment context, String url, final JSONObject postdata, String method_name) {
- mContext = ((Activity) context.getActivity());
- volley = (VolleyTasksListener) context;
- startVollyTask(url, postdata, method_name);
- }
- public static void makeVolleyPost(final Activity context, String url, final JSONObject postdata, String method_name) {
- mContext = context;
- volley = (VolleyTasksListener) context;
- startVollyTask(url, postdata, method_name);
- }
- private static void startVollyTask(String url, final JSONObject postdata, final String method_name) {
- JsonObjectRequest jsonObjReq = new JsonObjectRequest (Request.Method.POST,
- url, postdata, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- L.d(response.toString());
- if (response != null) {
- volley.handleResult(method_name, response);
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.d (TAG, "Error: " + error.getMessage ());
- volley.handleError(error);
- }
- });
- // Adding request to request queue
- ImarqApplication.getInstance ().addToRequestQueue (jsonObjReq);
- L.i(url);
- L.i(postdata.toString());
- }
- public static void makeVolleyGETObject(final Activity context, String url, final String method_name) {
- volley = (VolleyTasksListener) context;
- JsonObjectRequest jsonObjReq = new JsonObjectRequest (Request.Method.GET,
- url, null, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- L.d(response.toString());
- volley.handleResult(method_name, response);
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.d (TAG, "Error: " + error.getMessage ());
- L.ToastMessage(context, "Sorry for the inconvenience. Please try again.");
- }
- });
- // Adding request to request queue
- ImarqApplication.getInstance().addToRequestQueue(jsonObjReq);
- }
- public static void makeVolleyGETObject(final Fragment context, String url, final String method_name) {
- volley = (VolleyTasksListener) context;
- JsonObjectRequest jsonObjReq = new JsonObjectRequest (Request.Method.GET,
- url, null, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- L.d(response.toString());
- volley.handleResult(method_name, response);
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.d (TAG, "Error: " + error.getMessage ());
- Toast.makeText(context.getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
- // hide the progress dialog
- }
- });
- // Adding request to request queue
- ImarqApplication.getInstance().addToRequestQueue(jsonObjReq);
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement