gpt4 book ai didi

java - 构建类似于 httpbuilder 的异步 httpbuilder

转载 作者:行者123 更新时间:2023-12-01 13:41:11 27 4
gpt4 key购买 nike

可能是发布这个的错误地方,但我一直在摆弄异步http构建器,试图让基本的密码查询工作。它可以与 Http Builders 一起使用,但无法使其与异步版本一起使用。

   @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' )
@Grab(group='net.sf.json-lib', module='json-lib', version='2.4', classifier='jdk15' )

import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def query(statement, params,success, error) {
def http = new HTTPBuilder( 'http://localhost:7474' )
http.request( POST, JSON ) {
uri.path = '/db/data/cypher/'
headers.'X-Stream' = 'true'
requestContentType = JSON
body = [ query : statement , params : params ?: [:] ]

// uri.query = [ param : 'value' ]

response.success = { resp, json ->
if (success) success(json)
else {
println "Status ${resp.statusLine} Columns ${json.columns}\nData: ${json.data}"
}
}

response.failure = { resp, message ->
def result=[status:resp.statusLine.statusCode,statusText:resp.statusLine.reasonPhrase]
result.headers = resp.headers.collect { h -> [ (h.name) : h.value ] }
result.message = message
if (error) {
error(result)
} else {
println "Status: ${result.status} : ${result.statusText} "
println 'Headers: ${result.headers}'
println 'Message: ${result.message}'
}
}
}
}

query("MATCH n RETURN n;",[],{ println "Success: ${it}" },{ println "Error: ${it}" })

但是我已经用AsyncHttpBuilder尝试过这个。无法让它工作。现在我正在尝试一个简单的事情,但无法让它给出任何类型的有用结果。

@Test
public void testQueue()
{
def http = new AsyncHTTPBuilder( poolSize : 1 ,
uri : 'http://localhost:7474/db/data/cypher' )
def responses = []
responses << http.post(query : [q: "MATCH n RETURN n;"]) {return it}


if (!responses.every{it.done})
{
println 'waiting...'
Thread.sleep(2000)
}
responses.each {
println(it)
}
http.shutdown()
}

有什么想法吗?谢谢!

最佳答案

供引用:我已在 https://groups.google.com/forum/?fromgroups#!topic/neo4j/5Cle5vBsMXQ 上回答过这个问题

you need to pass in the cypher query in the request's body and not as query param. See https://gist.github.com/sarmbruster/8114445 for a working example

关于java - 构建类似于 httpbuilder 的异步 httpbuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20761305/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com