Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.projetofinal.sisodonto.view;
- import com.projetofinal.sisodonto.controller.LoginController;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import com.projetofinal.sisodonto.view.R;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.AsyncHttpResponseHandler;
- import com.loopj.android.http.RequestParams;
- public class LoginActivity extends Activity implements OnClickListener{
- private Button btEntrar;
- private LoginController controller;
- private EditText etEmail;
- private EditText etSenha;
- private Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- inicializarComponentes();
- inicializarController();
- }
- public void inicializarComponentes(){
- btEntrar = (Button)findViewById(R.id.btEntrar);
- btEntrar.setOnClickListener(this);
- etEmail = (EditText)findViewById(R.id.etEmail);
- etSenha = (EditText)findViewById(R.id.etSenha);
- }
- public void inicializarController(){
- this.controller = new LoginController(this);
- }
- public void invokeWS(RequestParams params){
- // Make RESTful webservice call using AsyncHttpClient object
- AsyncHttpClient client = new AsyncHttpClient();
- client.get("http://192.168.2.2:9999/useraccount/login/dologin",params ,new AsyncHttpResponseHandler() {
- // When the response returned by REST has Http response code '200'
- @Override
- public void onSuccess(String response) {
- try {
- // JSON Object
- JSONObject obj = new JSONObject(response);
- // When the JSON response has status boolean value assigned with true
- if(obj.getBoolean("status")){
- Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
- // Navigate to Home screen
- }
- // Else display error message
- else{
- Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
- e.printStackTrace();
- }
- }
- // When the response returned by REST has Http response code other than '200'
- @Override
- public void onFailure(int statusCode, Throwable error,
- String content) {
- // When Http response code is '404'
- if(statusCode == 404){
- Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
- }
- // When Http response code is '500'
- else if(statusCode == 500){
- Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
- }
- // When Http response code other than 404, 500
- else{
- 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();
- }
- }
- });
- }
- public void loginUser(View view){
- String email = etEmail.getText().toString();
- String password = etSenha.getText().toString();
- RequestParams params = new RequestParams();
- if(Utility.isNotNull(email) && Utility.isNotNull(password)){
- // When Email entered is Valid
- if(Utility.validate(email)){
- // Put Http parameter username with value of Email Edit View control
- params.put("username", email);
- // Put Http parameter password with value of Password Edit Value control
- params.put("password", password);
- // Invoke RESTful Web Service with Http parameters
- invokeWS(params);
- }
- // When Email is invalid
- else{
- Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
- }
- } else{
- Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_LONG).show();
- }
- }
- public void navigatetoTrocarSenhaActivity(){
- Intent i = new Intent(this.context, TrocarSenhaActivity.class);
- context.startActivity(i);
- }
- public void onClick(View v){
- switch(v.getId()){
- case R.id.btEntrar:
- //controller.entrar();
- this.loginUser(v);
- this.navigatetoTrocarSenhaActivity();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment