2011-07-14 151 views
0

我得到一个奇怪的问题,我困在它至少4个小时了。其实我已经写了我的代码在一个控制器进行测试,但是当我将代码移到服务时,我得到一个奇怪的行为,即服务中的方法没有返回,或者可能是在服务中调用它们的方法只接收不到。退货不能在服务

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; 
} 

不知道resp在哪里?请提供任何建议? OK我知道的罪魁祸首,我张贴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") 
} 

确定什么是错在上面,我不知道是什么任何人都可以请帮助?

+1

你能后的实际代码? –

+1

您必须在可见性范围内具有其他名称为'methodB'的其他名称。闭包,变量,不管。实际代码为+1。 –

+0

嗨胜利者,蒂姆:对于迟到的回应抱歉,但没有这样的..只是从一个UtilController我打电话给服务方法A,并从该方法A在服务我打电话给另一个methodB方法。现在一切都很好,直到methodB返回的东西,它在methodA中不可用,但是如果我在UtilController中写入methodB,我得到返回值..我认为它与我的服务有关,因为它实现了InitializingBean,GroovyInterceptable –

回答

1

invokeMethod和服务的代码似乎并不相同,除非有单独的FacebookGraphService。假设是这种情况,那么resp正在if (calledMethod) {区块内的invokeMethod部分中被捕获,但由于它不是该方法的最后一行,因此它不会从调用invokeMethod返回,因此正在被吞噬。

尝试增加一回calledMethod.invoke(this, args)

if(calledMethod){ 
     System.out.println("IN IF AND INVOKING METHOD ${calledMethod}") 
     return calledMethod.invoke(this, args) 
    } 
+0

非常棒非常感谢你plecong。 –