I am getting a wierd problem and i am stuck on it for atleast 4 hours now. Actually i had written my code in a controller for testing but when i have moved the code to service i am getting a strange behaviour that the methods in service are not returning or may be methods that are calling them in the service only are not receiving .
class FacebookService implements InitializingBean,开发者_运维问答 GroovyInterceptable {
def getUserLikes(def at){
List<String> listOfUrls = []
String basicFbUrl = "https://graph.facebook.com/"
String likeUrl = basicFbUrl + "me/likes?access_token=${at}"
URL url = new URL(likeUrl)
String jsonResponse = getResponseFromUrl(url)
println "JSON RESPONSE IS ${jsonResponse}" // this is showing null
}
String getResponseFromUrl() {
String something
String resp = null;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
int respCode = conn.responseCode
if (respCode == 400) {
log.error("COULD NOT MAKE CONNECTION")
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
def jsonResp = JSON.parse(br.text)
} else {
resp = conn.getInputStream().getText()
}
} finally {
conn.disconnect()
}
println("RETURNIG RESPONSE ${resp}") // This returns me a map as expected
return resp;
}
Dont know where does resp goes ?? any suggestions please ?? OK i know the culprit , i am posting the code of invokeMethod
def invokeMethod(String name, args){
System.out.println("IN INVOKE METHOD NAME ${name}")
if(facebookPalsCache==null)
facebookPalsCache = new FacebookPalsCache(1000)
System.out.println("time before ${name} called: ${new Date()}")
//Get the method that was originally called.
def calledMethod = metaClass.getMetaMethod(name, args)
System.out.println("CALLED METHOD IS ${calledMethod}")
//The "?" operator first checks to see that the "calledMethod" is not
//null (i.e. it exists).
if(name.equals("getFriends")){
println "getFriends..."
def session = RequestContextHolder.currentRequestAttributes().getSession()
def friends = facebookPalsCache.get(session.facebook.uid)
if(!friends){
def getFriends = facebookGraphService.invokeMethod (name, args)
println "Saving FBFRIENDS in CACHE"
facebookPalsCache.put(session.facebook.uid, getFriends)
return getFriends
}
else return friends
}
else {
if(calledMethod){
System.out.println("IN IF AND INVOKING METHOD ${calledMethod}")
calledMethod.invoke(this, args)
}
else {
return facebookGraphService.invokeMethod(name, args)
}
}
System.out.println "RETURNING FROM INVOKE METHOD FOR NAME ${name}"
System.out.println("time after ${name} called: ${new Date()}\n")
}
OK SOMETHING IS WRONG ABOVE I DONT KNOW WHAT CAN ANYONE PLEASE HELP ??
The code for invokeMethod
and the service don't seem to be the same unless there's a separate FacebookGraphService
. Assuming that's the case, then the resp
is being caught in the part of your invokeMethod
that's inside the if (calledMethod) {
block, but since it's not the last line of the method it's not being returned from the call to invokeMethod
and therefore is being gobbled up.
Try adding a return to calledMethod.invoke(this, args)
:
if(calledMethod){
System.out.println("IN IF AND INVOKING METHOD ${calledMethod}")
return calledMethod.invoke(this, args)
}
精彩评论