简体   繁体   中英

Call a interface method from Android activity class

Below is the interface class

public interface ICasAuthentication {
    String getTicketGrantingTicket(String username, String password, String hostUrl, String casUrl);
}

This is the class method which implements the interface.

public class CasAuthentication  implements ICasAuthentication{
    private IServiceHandler _iServiceHandler = null;
    public CasAuthentication(IServiceHandler iServiceHandler)
    {
        this._iServiceHandler = iServiceHandler;

    }
}

 public String getTicketGrantingTicket(String username, String password, String hostUrl, String casUrl) {
        Map<String,String> postData = new HashMap<String,String>();
        postData.put("username", username);
        postData.put("password", password);
        postData.put("hostUrl", hostUrl);
       String response = _iServiceHandler.makeServiceCallString(casUrl, MethodTypes.POST.toString(), postData);
        return null;
    }
}

Now I have the android activity class as code below. I want to call ICasAuthentication method from my android class. How can i achieve this task.

public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        String username = "test";
        String Password = "test2";
        String hostUrl= "";
        String casUrl= "";
    }
}

for calling the interface method you have to create object of the implemented class, and then you can refer to the function using that object. example:

 public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor>,IServiceHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ............
        ............
        ............
        CasAuthentication casAuthentication = new CasAuthentication(this);
        String expectedResult = casAuthentication.getTicketGrantingTicket(username, password, hostUrl, casUrl)

    }}

您应该尝试为此实现模型类

If I understand you correctly, you could just instantiate the class and call the method like this:

ICasAuthentication myClass = newCasAuthentication(new IServiceHandler());
myClass.getTicketGrandingTicket(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM