Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 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.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. // Instantiate Http Request Param Object
  72. RequestParams params = new RequestParams();
  73.  
  74. // When Email Edit View and Password Edit View have values other than Null
  75. if (Utility.isNotNull(email) && Utility.isNotNull(password)) {
  76. // When Email entered is Valid
  77. if (Utility.validate(email)) {
  78. // Put Http parameter username with value of Email Edit View control
  79. params.put("Username", email);
  80.  
  81.  
  82. // Put Http parameter password with value of Password Edit Value control
  83. params.put("Pass", password);
  84.  
  85.  
  86. // Invoke RESTful Web Service with Http parameters
  87. Login(params);
  88. }
  89. // When Email is invalid
  90. else {
  91. Toast.makeText(getApplicationContext(), "Please enter valid email address with no spaces!", Toast.LENGTH_LONG).show();
  92. }
  93. } else {
  94. Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank!", Toast.LENGTH_LONG).show();
  95. }
  96.  
  97.  
  98. }
  99.  
  100.  
  101.  
  102. /**
  103. * Method that performs RESTful webservice invocations
  104. *
  105. * @param params
  106. */
  107. public void Login(RequestParams params){
  108. // Show Progress Dialog
  109. prgDialog.show();
  110.  
  111.  
  112.  
  113. // Make RESTful webservice call using AsyncHttpClient object
  114. final AsyncHttpClient client = new AsyncHttpClient();
  115. client.get("MY PRIVATE URL IS HERE", params, new AsyncHttpResponseHandler() {
  116.  
  117.  
  118.  
  119. // When the response returned by REST has Http response code '200'
  120. @Override
  121. public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
  122. {
  123.  
  124. // Hide Progress Dialog
  125. prgDialog.hide();
  126. try {
  127.  
  128.  
  129.  
  130.  
  131. JSONObject obj = new JSONObject("{Username:aharvey@gmail.com, 'Pass:Harvey1214'}");
  132.  
  133.  
  134. // When the JSON response has status boolean value assigned with true
  135. if (obj.getBoolean("status")) {
  136. Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
  137. // Navigate to Home screen
  138. navigatetoMyLocationActivity();
  139. }
  140. // Else display error message
  141. else {
  142. errorMsg.setText(obj.getString("error_msg"));
  143. Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
  144. }
  145. } catch (JSONException e) {
  146. // TODO Auto-generated catch block
  147. Toast.makeText(getApplicationContext(), "Error Occurred [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
  148. e.printStackTrace();
  149.  
  150. }
  151.  
  152. }
  153.  
  154.  
  155. // When the response returned by REST has Http response code other than '200'
  156. @Override
  157. public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
  158. // Hide Progress Dialog
  159. prgDialog.hide();
  160. // When Http response code is '404'
  161. if (statusCode == 404) {
  162. Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
  163. }
  164. // When Http response code is '500'
  165. else if (statusCode == 500) {
  166. Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
  167. }
  168. // When Http response code other than 404, 500
  169. else {
  170. 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();
  171. }
  172.  
  173. }
  174. });
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /**
  181. * Method which navigates from Login Activity to Home Activity
  182. */
  183. public void navigatetoMyLocationActivity(){
  184. Intent homeIntent = new Intent(getApplicationContext(),MyLocation.class);
  185. homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  186. startActivity(homeIntent);
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement