Use data from another class

1

I have this method:

    public String comprovarLogin(String usuari, String contrasenya){
    final String metode = "comprovarLogin";
    String ret = null;
    try{         
        //Preparem els arguments del mètode compovarLogin
        Map<String,String> args = new HashMap<>();
        args.put("usuari",usuari);
        args.put("contrasenya",contrasenya);
        //Fem la crida al mètode per obtenir-ne la resposta
        SoapObject respostaSoap = usarWebService(metode,args);
        //Retornem el valor de "authId"   
        System.out.println(respostaSoap.getPrimitiveProperty("authId"));


    }
    catch(Exception ex){
        ex.printStackTrace();
        ret = "ERROR_CRIDA";
    }
    return ret;
}

in the WbServiceCalls class

The issue is that from another class called Xxxxxxx

I do the following:

     WebServiceCalls calls = new WebServiceCalls();
         System.out.println(calls.comprovarLogin(usuari, usuariContrasenya));

I call the comprovarLogin method and I pass two values to it.

Up to here perfect. But I would be interested to be able to use the variable respostaSoap.getPrimiteProperty ("authId") of the class webserviceCalls to do things with it from class xxxxxxx.

How can I do it?

The idea is to make an if / else to verify that the authId value is for example the name "Dani".

Thanks.

PS: My idea would be to do something like this:

     WebServiceCalls calls = new WebServiceCalls();
         System.out.println(calls.comprovarLogin(usuari, usuariContrasenya));
//comprobar authId
if (Authid.equalsIgnoreCase("Dani"){
bla bla bla
{
}else{
bla bla 
}
    
asked by Montse Mkd 16.11.2018 в 16:33
source

2 answers

3

It makes me curious that you return null when everything goes well and an error message that does not really say anything special when the login fails. Why do not you return what you need ?:

public SoapObject comprovarLogin(String usuari, String contrasenya){
    final String metode = "comprovarLogin";
    SoapObject respostaSoap = null;
    try{         
        //Preparem els arguments del mètode compovarLogin
        Map<String,String> args = new HashMap<>();
        args.put("usuari",usuari);
        args.put("contrasenya",contrasenya);
        //Fem la crida al mètode per obtenir-ne la resposta
        SoapObject respostaSoap = usarWebService(metode,args);
        //Retornem el valor de "authId"   
        System.out.println(respostaSoap.getPrimitiveProperty("authId"));


    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return respostaSoap;
}

The use would be something like:

WebServiceCalls calls = new WebServiceCalls();
SoapObject resposta = calls.comprovarLogin(usuari, usuariContrasenya));
if (resposta == null) {
    //error
} else {
    String authId = resposta.getPrimitiveProperty("authId"));
    ...
}
    
answered by 16.11.2018 / 17:13
source
1

I told you ...

There are several ways to do what you say, but what you want to do can be helped by this:

  • We assume that you have two classes, in this example, one the name Current and the other Other .
  • In the Current class, create a static public variable public static string authId = ""; this allows you to access it from another class, remembering that the class must be public. Note: Remember that static variables have a lifetime that extends throughout the execution of the program. Now assign the value you want to that variable to persist during the execution of the program, example: authId = respostaSoap.getPrimitiveProperty("authId"); .
  • Now, just use it in the Other class, example: String strAuthId = Actual.authId; or use a getter method.
  • I share your code with the aforementioned:

    public class Actual
    {
            public static String authId = "";
    
            public String comprobarLogin(String usuari, String contrasenya)
            {
    
                final String metode = "comprovarLogin";
                String ret = "";//no entiendo porque le tienes null acá
    
                try{                            
                    Map<String,String> args = new HashMap<>();
                    args.put("usuari",usuari);
                    args.put("contrasenya",contrasenya);
                    SoapObject respostaSoap = usarWebService(metode,args);  
                    System.out.println(respostaSoap.getPrimitiveProperty("authId"));
    
                    authId = respostaSoap.getPrimitiveProperty("authId").toString(); //por ejemplo
    
                }
    
                catch(Exception ex){
                    ex.printStackTrace();
                    ret = "ERROR_CRIDA";
                }
    
                return ret;
            }
    }
    
    public class Other
    {
        String strAuthId  = Actual.authId;
    
        WebServiceCalls calls = new WebServiceCalls();
        System.out.println(calls.comprobarLogin(usuari, usuariContrasenya));
        //comprobar authId
        if (strAuthId.equalsIgnoreCase("Dani"))
        {
            //Code
        {
        }
        else
        {
            //Code
        }
    }
    

    As I commented to you at the beginning, you have several ways of doing it, I hope it helps you to take an idea,

    Good luck ...

        
    answered by 16.11.2018 в 17:06