Advertisement
Guest User

Untitled

a guest
Feb 10th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. package com.projetofinal.sisodonto.view;
  2.  
  3. import com.projetofinal.sisodonto.controller.LoginController;
  4.  
  5. import android.app.ProgressDialog;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.app.Activity;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.projetofinal.sisodonto.view.R;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import com.loopj.android.http.AsyncHttpClient;
  22. import com.loopj.android.http.AsyncHttpResponseHandler;
  23. import com.loopj.android.http.RequestParams;
  24.  
  25. import cz.msebera.android.httpclient.Header;
  26.  
  27.  
  28. public class LoginActivity extends Activity implements OnClickListener{
  29.    
  30.     private Button btEntrar;
  31.     private LoginController controller;
  32.     private EditText etEmail;
  33.     private EditText etSenha;
  34.     ProgressDialog prgDialog;
  35.     TextView errorMsg;
  36.     // Email Edit View Object
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_login);
  42.        
  43.         inicializarComponentes();
  44.         inicializarController();
  45.     }
  46.  
  47.     public void inicializarComponentes(){
  48.         btEntrar = (Button)findViewById(R.id.btEntrar);
  49.         btEntrar.setOnClickListener(this);
  50.  
  51.         etEmail = (EditText)findViewById(R.id.etEmail);
  52.         etSenha = (EditText)findViewById(R.id.etSenha);
  53.  
  54.         // Find Error Msg Text View control by ID
  55.         errorMsg = (TextView)findViewById(R.id.login_error);
  56.         // Instantiate Progress Dialog object
  57.         prgDialog = new ProgressDialog(this);
  58.         // Set Progress Dialog Text
  59.         prgDialog.setMessage("Please wait...");
  60.         // Set Cancelable as False
  61.         prgDialog.setCancelable(false);
  62.     }
  63.  
  64.     public void invokeWS(View view) {
  65.         String email = etEmail.getText().toString();
  66.         // Get Password Edit View Value
  67.         String password = etSenha.getText().toString();
  68.         // Instantiate Http Request Param Object
  69.         RequestParams params = new RequestParams();
  70.         // When Email Edit View and Password Edit View have values other than Null
  71.         if(Utility.isNotNull(email) && Utility.isNotNull(password)){
  72.             // When Email entered is Valid
  73.             if(Utility.validate(email)){
  74.                 // Put Http parameter username with value of Email Edit View control
  75.                 params.put("username", email);
  76.                 // Put Http parameter password with value of Password Edit Value control
  77.                 params.put("password", password);
  78.                 // Invoke RESTful Web Service with Http parameters
  79.                 invokeWS(params);
  80.             }
  81.             // When Email is invalid
  82.             else{
  83.                 Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
  84.             }
  85.         }
  86.         // When any of the Edit View control left blank
  87.         else{
  88.             Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_LONG).show();
  89.         }
  90.  
  91.  
  92.     }
  93.  
  94.     public void navigatetoHomeActivity(){
  95.         Intent homeIntent = new Intent(getApplicationContext(),MainMenuActivity.class);
  96.         homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  97.         startActivity(homeIntent);
  98.     }
  99.  
  100.     //public void loginUser(View view){
  101.  
  102.     //}
  103.    
  104.     public void inicializarController(){
  105.  
  106.         this.controller = new LoginController(this);
  107.     }
  108.    
  109.     public void onClick(View v){
  110.  
  111.  
  112.     }
  113.  
  114.     public void invokeWS(RequestParams params){
  115.         // Show Progress Dialog
  116.         prgDialog.show();
  117.         // Make RESTful webservice call using AsyncHttpClient object
  118.         AsyncHttpClient client = new AsyncHttpClient();
  119.         client.get("http://192.168.43.17:9999/useraccount/login/dologin",params ,new AsyncHttpResponseHandler() {
  120.  
  121.             /** @deprecated */
  122.             @Deprecated
  123.             public final void onSuccess(String content) {
  124.                 // Hide Progress Dialog
  125.                 prgDialog.hide();
  126.                 try {
  127.                     // JSON Object
  128.                     JSONObject obj = new JSONObject(content);
  129.                     // When the JSON response has status boolean value assigned with true
  130.                     if(obj.getBoolean("status")){
  131.                         Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
  132.                         // Navigate to Home screen
  133.                         navigatetoHomeActivity();
  134.                     }
  135.                     // Else display error message
  136.                     else{
  137.                         errorMsg.setText(obj.getString("error_msg"));
  138.                         Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
  139.                     }
  140.                 } catch (JSONException e) {
  141.                     // TODO Auto-generated catch block
  142.                     Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
  143.                     e.printStackTrace();
  144.  
  145.                 }
  146.             }
  147.             // When the response returned by REST has Http response code other than '200'
  148.  
  149.             /** @deprecated */
  150.             @Deprecated
  151.             public final void onFailure(int statusCode, Throwable error, String content) {
  152.                 // Hide Progress Dialog
  153.                 prgDialog.hide();
  154.                 // When Http response code is '404'
  155.                 if(statusCode == 404){
  156.                     Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
  157.                 }
  158.                 // When Http response code is '500'
  159.                 else if(statusCode == 500){
  160.                     Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
  161.                 }
  162.                 // When Http response code other than 404, 500
  163.                 else{
  164.                     Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
  165.                 }
  166.             }
  167.             }
  168.         });
  169.     }
  170.    
  171.     @Override
  172.     public boolean onCreateOptionsMenu(Menu menu) {
  173.         // Inflate the menu; this adds items to the action bar if it is present.
  174.         getMenuInflater().inflate(R.menu.login, menu);
  175.         return true;
  176.     }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement