我正在创建这个方法/函数,我需要实现回调。我的意思是,我需要添加一个函数作为动态参数。 我已经阅读了几篇文章,但我不明白如何获得它。 有任何想法或使用示例吗?
public void httpReq (final String url, final Object postData, String callbackFunct, Object callbackParam,String callbackFailFunct) {
if (postData == null || postData == "") {
//GET
Thread testGET = new Thread(new Runnable() {
@Override
public void run() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
....
}
}
} else {
//POST
Thread testPOST = new Thread(new Runnable() {
@Override
public void run() {
HttpGet httpPost = new HttpPost(url);
....
}
}
}
}
最佳答案
定义你的界面:
public interface MyInterface {
public void myMethod();
}
将其添加为您的方法的参数
public void httpReq (final String url, final Object postData, String callbackFunct, Object callbackParam,String callbackFailFunct, MyInterface myInterface) {
// when the condition happens you can call myInterface.myMethod();
}
当您调用您的方法时,例如,
myObjec.httpReq(url, postData, callbackFunct, callbackParam, callbackFailFunct,
new MyInterface() {
@Override
public void myMethod() {
}
});
这是你需要的吗?
关于java - 如何创建回调(添加为动态参数,一个函数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16613217/