개발/프로그래밍
Groovy
JackSama
2012. 4. 10. 03:37
1. html작성
html.html {
head {
title("Hello, Groovy")
}
body {
p("Hello, Groovy World!")
}
}
2. json사용법
def json = new JsonBuilder()
json.state {
a "111"
b "222"
}
println json.toPrettyString()
String jsonTxt = """
{
"state": {
"a": "111",
"b": "222"
}
}
"""
def slurper = new JsonSlurper()
def states = slurper.parseText(jsonTxt)
states.state.each
{
println it
}
3. simple http request
def query = "a=111&b=222";def url = new URL("http://code.google.com/p/groovy-http")
def conn = url.openConnection()
conn.setRequestMethod("POST")
conn.doOutput = true
Writer writer = new OutputStreamWriter(conn.outputStream)
writer.write(query)
writer.flush()
writer.close()
conn.connect()
println conn.content.text
4. http builder사용법
def http = new HTTPBuilder() uri.path = '/ajax/services/search/web' uri.query = [ v: '1.0' , q: 'Calvin and Hobbes' ] headers. 'User-Agent' = "Mozilla/5.0 Firefox/3.0.4" headers.Accept = 'application/json' response.success = { resp, reader -> assert resp.statusLine.statusCode == 200 println "Got response: ${resp.statusLine}" println "Content-Type: ${resp.headers.'Content-Type'}" println reader.text } response. '404' = { println 'Not found' } } |
http://groovy.codehaus.org/modules/http-builder/doc/get.html