Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. public class LoginFragment extends Fragment {
  2.  
  3. private static final String TAG = "LoginFragment";
  4.  
  5. EditText mUsername, mPassword;
  6. Button mLogin;
  7. String url = "abc";
  8.  
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  11. ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_login, container, false);
  12.  
  13. mUsername = (EditText) rootView.findViewById(R.id.login_name_edit_text);
  14. mPassword = (EditText) rootView.findViewById(R.id.login_password_edit_text);
  15. mLogin = (Button) rootView.findViewById(R.id.login_button);
  16.  
  17. mLogin.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View view) {
  20. String username = mUsername.toString();
  21. String password = mPassword.toString();
  22.  
  23. if (username.trim().length() > 0 && password.trim().length() > 0) {
  24. checkLogin(username, password);
  25. } else {
  26. Toast.makeText(getContext(), "Please enter the credentials!", Toast.LENGTH_SHORT).show();
  27. }
  28. }
  29. });
  30.  
  31. return rootView;
  32. }
  33.  
  34. private void checkLogin(final String username, final String password) {
  35. // Tag used to cancel the request
  36.  
  37. StringRequest strReq = new StringRequest(Request.Method.POST,
  38. url, new Response.Listener<String>() {
  39.  
  40. @Override
  41. public void onResponse(String response) {
  42. Log.d(TAG, "Login Response: " + response.toString());
  43.  
  44. try {
  45. JSONObject jObj = new JSONObject(response);
  46. boolean error = jObj.getBoolean("error");
  47.  
  48. // Check for error node in json
  49. if (!error) {
  50. // user successfully logged in
  51.  
  52. // Launch main activity
  53. Intent intent = new Intent(getActivity(),
  54. MainActivity.class);
  55. startActivity(intent);
  56. getActivity().finish();
  57. } else {
  58. // Error in login. Get the error message
  59. String errorMsg = jObj.getString("message");
  60. Toast.makeText(getContext(),
  61. errorMsg, Toast.LENGTH_LONG).show();
  62. }
  63. } catch (JSONException e) {
  64. // JSON error
  65. e.printStackTrace();
  66. }
  67.  
  68. }
  69. }, new Response.ErrorListener() {
  70.  
  71. @Override
  72. public void onErrorResponse(VolleyError error) {
  73. Log.e(TAG, "Login Error: " + error.getMessage());
  74. Toast.makeText(getContext(),
  75. error.getMessage(), Toast.LENGTH_LONG).show();
  76. }
  77. }) {
  78.  
  79. @Override
  80. public String getBodyContentType() {
  81. return "application/x-www-form-urlencoded; charset=UTF-8";
  82. }
  83.  
  84. @Override
  85. protected Map<String, String> getParams() throws AuthFailureError {
  86. // Posting parameters to login url
  87. Map<String, String> params = new HashMap<String, String>();
  88. params.put("username", username);
  89. params.put("password", password);
  90.  
  91. return params;
  92. }
  93.  
  94. };
  95.  
  96. // Adding request to request queue
  97. MySingleton.getInstance(getContext()).addToRequestQueue(strReq);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement