How do I create a callback (added as a dynamic parameter, a function)?… here is a solution to the problem.
How do I create a callback (added as a dynamic parameter, a function)?
I’m creating this method/function and I need to implement the callback. I mean, I need to add a function as a dynamic parameter.
I’ve read several articles but I don’t understand how to get it.
Any ideas or examples of use?
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);
....
}
}
}
}
Solution
Define your interface:
public interface MyInterface {
public void myMethod();
}
Add it as a parameter to your method
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();
}
When you call your method, for example
myObjec.httpReq(url, postData, callbackFunct, callbackParam, callbackFailFunct,
new MyInterface() {
@Override
public void myMethod() {
}
});
Is this what you need?