2016-06-10 97 views
1

我有問題將json參數傳遞給web操作。我知道,在網絡行動工作在指定的URL http://projects.example.net/example/bugnetwebservice.asmx/MobileBuildAction,正如我與郵差與json的參數進行了測試:Groovy HttpBuilder json輸入問題

{ 
    featureIdStr: 31, 
    actionStr: 1, 
    comment: "Hello world" 
} 

,並獲得響應:

{ 
    "d": "Succeeded" 
} 

每當我嘗試運行它在Groovy中,但是,我得到這樣的迴應:

Jun 10, 2016 9:54:25 AM net.sf.json.JSONObject _fromBean 
INFO: Property 'value' of class org.codehaus.groovy.runtime.GStringImpl has no read method. SKIPPED 
Failure: 500 

這裏是我的代碼:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1') 
import groovyx.net.http.* 
import static groovyx.net.http.ContentType.* 
import static groovyx.net.http.Method.* 

def http = new HTTPBuilder("http://projects.example.net/") 
def issueId = 31 
def msg = "Build Failed" 
def jsonBody = [:] 
jsonBody.put("featureIdStr", issueId) 
jsonBody.put("actionStr", 0) 
jsonBody.put("comment", "${msg}: <a href='http://www.google.com'}'>Googles Job</a>") 
http.request(POST, JSON) { 
    uri.path = "/example/bugnetwebservice.asmx/MobileBuildAction" 
    body = jsonBody 

    response.success = { resp -> 
     println "Success! ${resp.status}" 
    } 

    response.failure = { resp -> 
     println "Failure: ${resp.status}" 
    } 
} 

請幫忙!

+0

你可以嘗試改變最後的'put'值' 「$ {}味精:Googles Job」 的ToString()'?我想我在HTTPBuilder中遇到了與String和GString處理有關的類似問題。 – bdkosher

回答

2

jsonBody.put( 「評論」, 「$ {}味精:http://www.google.com '}'>谷歌工作」)

的 「」 在Groovy中創建一個Groovy字符串(又名GString)。 GStrings非常棒 - 他們允許使用${}語法 - 但它們在序列化和反序列化過程中遇到了一些問題。有一個很棒的StackOverflow answer explaining what's up with that

無論如何,它的短是,這個職位和我自己的經驗之間:任何時候你比較或可能是序列化你的Groovy字符串,先請它toString()

我會考慮編寫代碼,如:

def commentValue = "${msg}: <a href='http://www.google.com'}'>Googles Job</a>" 

jsonBody.put(commentValue.toString())