Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. import org.json.JSONException;
  2. import org.json.JSONObject;
  3.  
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.loopj.android.http.AsyncHttpClient;
  14. import com.loopj.android.http.AsyncHttpResponseHandler;
  15. import com.loopj.android.http.RequestParams;
  16.  
  17.  
  18.  
  19. import cz.msebera.android.httpclient.Header;
  20.  
  21. public class LoginUser extends AppCompatActivity
  22. {
  23. // Progress Dialog Object
  24. ProgressDialog prgDialog;
  25. // Error Msg TextView Object
  26. TextView errorMsg;
  27. // Email Edit View Object
  28. EditText emailET;
  29. // Passwprd Edit View Object
  30. EditText pwdET;
  31.  
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.content_main);
  37. // Find Error Msg Text View control by ID
  38. errorMsg = (TextView) findViewById(R.id.login_error);
  39. // Find Email Edit View control by ID
  40. emailET = (EditText) findViewById(R.id.edtemail);
  41. // Find Password Edit View control by ID
  42. pwdET = (EditText) findViewById(R.id.edtpass);
  43. // Instantiate Progress Dialog object
  44. prgDialog = new ProgressDialog(this);
  45. // Set Progress Dialog Text
  46. prgDialog.setMessage("Please wait...");
  47. // Set Cancelable as False
  48. prgDialog.setCancelable(false);
  49. }
  50.  
  51.  
  52.  
  53.  
  54. /**
  55. * Method gets triggered when Login button is clicked
  56. *
  57. * @param view
  58. */
  59. public void loginUser(View view) {
  60. // Get Email Edit View Value
  61. String email = emailET.getText().toString();
  62. // Get Password Edit View Value
  63. String password = pwdET.getText().toString();
  64.  
  65. // Instantiate Http Request Param Object
  66. RequestParams params = new RequestParams();
  67.  
  68. // When Email Edit View and Password Edit View have values other than Null
  69. if (Utility.isNotNull(email) && Utility.isNotNull(password)) {
  70. // When Email entered is Valid
  71. if (Utility.validate(email)) {
  72. // Put Http parameter username with value of Email Edit View control
  73. params.put("Username", email);
  74. // Put Http parameter password with value of Password Edit Value control
  75. params.put("Pass", password);
  76. // Invoke RESTful Web Service with Http parameters
  77. Login(params);
  78. }
  79. // When Email is invalid
  80. else {
  81. Toast.makeText(getApplicationContext(), "Please enter valid email address with no spaces!", Toast.LENGTH_LONG).show();
  82. }
  83. } else {
  84. Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank!", Toast.LENGTH_LONG).show();
  85. }
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92. /**
  93. * Method that performs RESTful webservice invocations
  94. *
  95. * @param params
  96. */
  97. public void Login(RequestParams params){
  98. // Show Progress Dialog
  99. prgDialog.show();
  100.  
  101. // Make RESTful webservice call using AsyncHttpClient object
  102. final AsyncHttpClient client = new AsyncHttpClient();
  103. client.get("MY URL IS HERE", params, new AsyncHttpResponseHandler() {
  104.  
  105.  
  106.  
  107. // When the response returned by REST has Http response code '200'
  108. @Override
  109. public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
  110. {
  111.  
  112. // Hide Progress Dialog
  113. prgDialog.hide();
  114. try {
  115.  
  116. JSONObject obj = new JSONObject(new String(responseBody));
  117.  
  118. // When the JSON response has status boolean value assigned with true
  119. if (obj.getBoolean("status")) {
  120. Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
  121. // Navigate to Home screen
  122. navigatetoMyLocationActivity();
  123. }
  124. // Else display error message
  125. else {
  126. errorMsg.setText(obj.getString("error_msg"));
  127. Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
  128. }
  129. } catch (JSONException e) {
  130. // TODO Auto-generated catch block
  131. Toast.makeText(getApplicationContext(), "Error Occurred [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
  132. e.printStackTrace();
  133.  
  134. }
  135.  
  136. }
  137.  
  138.  
  139. // When the response returned by REST has Http response code other than '200'
  140. @Override
  141. public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
  142. // Hide Progress Dialog
  143. prgDialog.hide();
  144. // When Http response code is '404'
  145. if (statusCode == 404) {
  146. Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
  147. }
  148. // When Http response code is '500'
  149. else if (statusCode == 500) {
  150. Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
  151. }
  152. // When Http response code other than 404, 500
  153. else {
  154. Toast.makeText(getApplicationContext(), "Unexpected Error occurred! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
  155. }
  156.  
  157. }
  158. });
  159. }
  160.  
  161.  
  162.  
  163.  
  164. /**
  165. * Method which navigates from Login Activity to Home Activity
  166. */
  167. public void navigatetoMyLocationActivity(){
  168. Intent homeIntent = new Intent(getApplicationContext(),MyLocation.class);
  169. homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  170. startActivity(homeIntent);
  171. }
  172.  
  173. public class LoginUserDTO
  174. {
  175. private String UserName;
  176. private String Pass;
  177.  
  178.  
  179. public LoginUserDTO(String UserName, String Pass)
  180. {
  181. this.UserName = UserName;
  182. this.Pass = Pass;
  183.  
  184. }
  185.  
  186.  
  187. public String getUsername()
  188. {
  189. return UserName;
  190. }
  191.  
  192. public void setUserName(String UserName)
  193. {
  194. this.UserName = UserName;
  195. }
  196.  
  197.  
  198.  
  199. public String getPass()
  200. {
  201. return Pass;
  202. }
  203.  
  204. public void setPass(String Pass)
  205. {
  206. this.Pass = Pass;
  207. }
  208.  
  209. <uses-permission android:name="android.permission.INTERNET" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement