2016-05-25 15 views
0

getメソッド(HttpGetJson)を呼び出して応答値を返し、resp(応答コード用)およびjson(コンテンツ用)値を取得するために使用します。しかし、私はresponse.successとは別にrespとjsonを返すときだけ有効な出力を得ます。私が応答オブジェクトを返そうとすると、私はNULLだけを取得します。レスポンスを返す方法はありますか?HTTPBuilderを使用したREST APIの応答の取得

public static Object HttpGetJson(String url, String path) 
     def http = new HTTPBuilder(url) 

     //perform a GET request, expecting JSON response 
     http.request(GET,JSON) { req -> 
      uri.path = path 

      //response handler for a success response code 
      response.success = { resp, json -> 
       return response//using response instead of json or resp returns null 
      } 
     } 

public void testGET() 
{ 

    def url = 'testurl' 
    def path = 'testpath' 

    //submit a request through GET 
    def response1 = HttpUtils.HttpGetJson(url,path) 

    println response1 
    //println response.statusLine.statusCode 
} 

答えて

1

return [resp: resp, json: json]のように両方のマップを返すことができます。

static HttpGetJson(String url, String path, Closure success) { 
    def http = new HTTPBuilder(url) 

    //perform a GET request, expecting JSON response 
    http.request(GET,JSON) { req -> 
     uri.path = path 

     //response handler for a success response code 
     response.success = success 
    } 
} 

void testGET() { 
    def url = 'testurl' 
    def path = 'testpath' 

    //submit a request through GET 
    def response1 = HttpUtils.HttpGetJson(url,path) { resp, json -> 
     println resp 
     println json 
    } 

} 
+0

働い:


IMOあなたはresponse.successから実行するクロージャを渡す必要があります。ありがとう! – Rebeller

関連する問題