Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. public class LoginActivity extends BaseActivity implements VolleyTasksListener {
  2.  
  3. private EditText edtUser;
  4. private EditText edtPassword;
  5. private Button btnLogin;
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_login);
  11.  
  12. findViews();
  13. }
  14.  
  15. private void callLoginService() {
  16.  
  17. if (ConnectionUtils.isConnectedNetwork(this)) {
  18. if (!edtUser.getText().toString().equals("")) {
  19. if (!edtPassword.getText().toString().equals("")) {
  20.  
  21. JSONObject jsonMainObject = new JSONObject();
  22. try {
  23. jsonMainObject.put("username", edtUser.getText().toString());
  24. jsonMainObject.put("password", edtPassword.getText().toString());
  25. VolleyTasks.makeVolleyPost(LoginActivity.this, Constant.LOGIN_URL, jsonMainObject, "Login");
  26.  
  27. } catch (JSONException e) {
  28. e.printStackTrace();
  29. }
  30. } else {
  31. DialogUtils.showPositiveDialog(this, "", "Please enter valid Password");
  32.  
  33. }
  34. } else {
  35. DialogUtils.showPositiveDialog(this, "", "Please enter valid UserId ");
  36. }
  37. } else {
  38. DialogUtils.showPositiveDialog(this, "", getString(R.string.error_connection));
  39. }
  40. }
  41.  
  42. private void findViews() {
  43. edtUser = (EditText) findViewById(R.id.edt_user_id);
  44. edtPassword = (EditText) findViewById(R.id.edt_password);
  45. btnLogin = (Button) findViewById(R.id.btn_login);
  46.  
  47. btnLogin.setOnClickListener(this);
  48. }
  49.  
  50. @Override
  51. public void onClick(View v) {
  52. super.onClick(v);
  53.  
  54. switch (v.getId()) {
  55. case R.id.btn_login:
  56. callLoginService();
  57. break;
  58. }
  59.  
  60. }
  61.  
  62. @Override
  63. public void handleError(Exception error) {
  64.  
  65. L.d("error" + error);}
  66.  
  67. @Override
  68. public void handleResult(String method_name, JSONObject response) {
  69.  
  70. L.d("response" + response);
  71. if (response != null) {
  72. GsonBuilder gsonBuilder = new GsonBuilder();
  73. gsonBuilder.serializeNulls();
  74. Gson gson = gsonBuilder.create();
  75. if (method_name.equals("Login")) {
  76. AbstractLogin abstractLogin = gson.fromJson(response.toString(), AbstractLogin.class);
  77. if (abstractLogin != null) {
  78. if (abstractLogin.getApi().equals("UserLogin")) {
  79.  
  80. Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
  81. startActivity(intent);
  82. finish();
  83. } else {
  84. DialogUtils.showPositiveDialog(this, "", getString(R.string.err_msg_request_failed));
  85. }
  86. }
  87. }
  88. }
  89. }}
  90.  
  91. public class VolleyTasks {
  92.  
  93.  
  94. static VolleyTasksListener volley;
  95. private static String TAG = VolleyTasks.class.getSimpleName();
  96. private static Context mContext;
  97.  
  98. public static void makeVolleyPost(Fragment context, String url, final JSONObject postdata, String method_name) {
  99. mContext = ((Activity) context.getActivity());
  100. volley = (VolleyTasksListener) context;
  101. startVollyTask(url, postdata, method_name);
  102. }
  103.  
  104. public static void makeVolleyPost(final Activity context, String url, final JSONObject postdata, String method_name) {
  105. mContext = context;
  106. volley = (VolleyTasksListener) context;
  107. startVollyTask(url, postdata, method_name);
  108. }
  109.  
  110. private static void startVollyTask(String url, final JSONObject postdata, final String method_name) {
  111. JsonObjectRequest jsonObjReq = new JsonObjectRequest (Request.Method.POST,
  112. url, postdata, new Response.Listener<JSONObject>() {
  113.  
  114. @Override
  115. public void onResponse(JSONObject response) {
  116. L.d(response.toString());
  117. if (response != null) {
  118.  
  119. volley.handleResult(method_name, response);
  120.  
  121. }
  122. }
  123. }, new Response.ErrorListener() {
  124.  
  125. @Override
  126. public void onErrorResponse(VolleyError error) {
  127. VolleyLog.d (TAG, "Error: " + error.getMessage ());
  128.  
  129. volley.handleError(error);
  130. }
  131. });
  132.  
  133. // Adding request to request queue
  134. ImarqApplication.getInstance ().addToRequestQueue (jsonObjReq);
  135.  
  136. L.i(url);
  137. L.i(postdata.toString());
  138. }
  139.  
  140. public static void makeVolleyGETObject(final Activity context, String url, final String method_name) {
  141. volley = (VolleyTasksListener) context;
  142. JsonObjectRequest jsonObjReq = new JsonObjectRequest (Request.Method.GET,
  143. url, null, new Response.Listener<JSONObject>() {
  144.  
  145. @Override
  146. public void onResponse(JSONObject response) {
  147. L.d(response.toString());
  148.  
  149.  
  150.  
  151. volley.handleResult(method_name, response);
  152. }
  153. }, new Response.ErrorListener() {
  154.  
  155. @Override
  156. public void onErrorResponse(VolleyError error) {
  157. VolleyLog.d (TAG, "Error: " + error.getMessage ());
  158. L.ToastMessage(context, "Sorry for the inconvenience. Please try again.");
  159. }
  160. });
  161.  
  162. // Adding request to request queue
  163. ImarqApplication.getInstance().addToRequestQueue(jsonObjReq);
  164. }
  165.  
  166. public static void makeVolleyGETObject(final Fragment context, String url, final String method_name) {
  167. volley = (VolleyTasksListener) context;
  168.  
  169. JsonObjectRequest jsonObjReq = new JsonObjectRequest (Request.Method.GET,
  170. url, null, new Response.Listener<JSONObject>() {
  171.  
  172. @Override
  173. public void onResponse(JSONObject response) {
  174. L.d(response.toString());
  175.  
  176. volley.handleResult(method_name, response);
  177. }
  178. }, new Response.ErrorListener() {
  179.  
  180. @Override
  181. public void onErrorResponse(VolleyError error) {
  182. VolleyLog.d (TAG, "Error: " + error.getMessage ());
  183. Toast.makeText(context.getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
  184. // hide the progress dialog
  185. }
  186. });
  187.  
  188. // Adding request to request queue
  189. ImarqApplication.getInstance().addToRequestQueue(jsonObjReq);
  190. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement