Reputation: 115
I have a method witch is called as a parameter like the code below...I need to use the parameter "arry" outside the Oncomplete method, in my code..Is there a way that i can achieve that??
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
String arry = graphObject.getProperty("data").toString();
}
}
Upvotes: 0
Views: 199
Reputation: 1280
Firstly, I suggest you do whatever you need to inside the callback. The API seems to have been designed that way for some reason. Probably performance. Apparently onCompleted()
gets invoked asynchronously. As a result when you try to access arry
right after new Request()
returns (using a final
local variable) the value is still null.
But if you still need to do it here is a simple way.
final String result = null;
final CountDownLatch latch = new CountDownLatch(1);
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
String arry = graphObject.getProperty("data").toString();
result = arry; // Assign response
latch.countDown(); // Mark completion
}
}
latch.await(); // Wait for Request to complete
System.out.println(result); // Use result
Reiterating again, this will defeat Reqeust
purpose of being asynchronous and might hit the performance.
Upvotes: 1
Reputation: 18286
Create a class that extends (implements) Request.Callback and pass it to the method.
This class can store the String arry.
class RequestCallback extends Request.Callback {
private String arry;
public String getArry() {
return arry;
}
public void inCompleted(Response response) {
this.arry = graphObject.getProperty("data").toString();
}
}
Then:
RequestCallback callback = new RequestCallback();
Request request = new Request(session, "/fql", params, HttpMethod.GET, callback);
...
// after the request is completed
callback.getArry(); // and use it.
Upvotes: 4