Login with token Auth.

2533
1
02-19-2016 08:51 AM
EvelynHernandez
Occasional Contributor III

Hello,

Im developing an app that works with a secured rest service for loading layers and base maps.

My 2 Views are:

1.- activity_login: allows to access to the other view (map_activity) using token auth.

           if the credential given is correct

                    change to the next activity.

                    charge the modules associated to the account. (A list of modules in could be another view or something similar)

                    preload the layers to be used in the activity_map.

          else

                    shows a toast saying that ur credential is wrong or another error type.

2.- activity_map: where all the layers and map and another stuff is loaded and u are authenticated to see them.

Im having problems on the logic i think now. My code for login activity is:

public class Login extends AppCompatActivity{

   private Button btnIngresar;
   private EditText txtUsuario;
   private EditText txtPassword;
   public MapView myMapView;
  // public map classMapa = new map();

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_login);

   //getting object references:
   txtUsuario = (EditText)findViewById(R.id.TxtNombre);
   txtPassword = (EditText)findViewById(R.id.TxtPassword);
   btnIngresar = (Button)findViewById(R.id.btnIngreso);

   //getting values for arcgis services
   final String urlToken = this.getResources().getString(R.string.webToken);
   final String urlChq006 = this.getResources().getString(R.string.webChilquinta006);

   //getting values for toasts
   final String toLoginIncorrecto = this.getResources().getString(R.string.toastLoginIncorrecto);


   btnIngresar.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v){

   //Login with static credentials
  /*
  if(txtUsuario.getText().toString().equals("evelyn") && txtPassword.getText().toString().equals("asdf")){
  Intent intent = new Intent(Login.this,map.class);
  startActivity(intent);
  }else{
  Toast.makeText(Login.this,"Login Incorrecto",Toast.LENGTH_SHORT).show();
  }
  */

  //Login with token credentials



  //Set map and options
   myMapView = (MapView) findViewById(R.id.mimapa);
   //Set color de fondo mapa base
   myMapView.setMapBackground(0xffffff, 0xffffff, 10, 10);


   //Ingresando con credenciales
   final UserCredentials credencial = new UserCredentials();
  credencial.setUserAccount(txtUsuario.getText().toString(), txtPassword.getText().toString());
  credencial.setTokenServiceUrl(urlToken);

   final ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer(urlChq006,null,credencial);
   myMapView.addLayer(layer);

   //Cabios en el mapa
   myMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
   @Override
   public void onStatusChanged(Object o, STATUS status) {
   if (STATUS.LAYER_LOADED == status) {

  Toast.makeText(Login.this, "Logueando...", Toast.LENGTH_SHORT).show();
   //Cambiando al mapa
   Intent intent = new Intent(Login.this, map.class);
  startActivity(intent);
  }

   if (status == STATUS.LAYER_LOADING_FAILED) {
   // Check if a layer is failed to be loaded due to security
   if ((status.getError()) instanceof EsriSecurityException) {
  EsriSecurityException securityEx = (EsriSecurityException) status
  .getError();
   if (securityEx.getCode() == EsriSecurityException.AUTHENTICATION_FAILED)
  Toast.makeText(myMapView.getContext(),
   "Authentication Failed! Resubmit!",
  Toast.LENGTH_SHORT).show();
   else if (securityEx.getCode() == EsriSecurityException.TOKEN_INVALID)
  Toast.makeText(myMapView.getContext(),
   "Invalid Token! Resubmit!",
  Toast.LENGTH_SHORT).show();
   else if (securityEx.getCode() == EsriSecurityException.TOKEN_SERVICE_NOT_FOUND)
  Toast.makeText(myMapView.getContext(),
   "Token Service Not Found! Resubmit!",
  Toast.LENGTH_SHORT).show();
   else if (securityEx.getCode() == EsriSecurityException.UNTRUSTED_SERVER_CERTIFICATE)
  Toast.makeText(myMapView.getContext(),
   "Untrusted Host! Resubmit!",
  Toast.LENGTH_SHORT).show();

   if (o instanceof ArcGISFeatureLayer) {
   // Set user credential through username and password

   credencial.setUserAccount(txtUsuario.getText().toString(), txtPassword.getText().toString());
   layer.reinitializeLayer(credencial);
  }
  }
  }
  }
  });

  }
  });

  }
}

My map class just have the following:

public class map extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

  setContentView(R.layout.content_map);

  }

}

for XML files

i have the map in activity_map.xml

i have the login (button, edittexts and etc in the activity_login)

The error that i have is with the map.

0 Kudos
1 Reply
WillCrick
Occasional Contributor

To provide you with help, people will probably need more information as to what the error actually is and why it is not working as expected.

0 Kudos