gpt4 book ai didi

java - 如何从Java代码调用couchDB View ?

转载 作者:行者123 更新时间:2023-12-01 12:59:42 25 4
gpt4 key购买 nike

我从我的java代码中调用couchDB View ,代码如下。

我将 CouchDB View URL 传递给 HttpGet() 方法并能够获取数据。

    HttpGet get = new HttpGet("http://localhost:5984/tp/_design/tp/_view/tp?startkey=1388607960000");

HttpResponse response = httpclient.execute(get);

HttpEntity entity=response.getEntity();

InputStream instream = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(instream));

String strdata = null;

String jsonString = "" ;

while( (strdata =reader.readLine())!=null)
{
jsonString += strdata;
}

但现在要求发生了变化,希望再向 View URL 传递一个参数,如下所示:

   http://localhost:5984/tp/_design/tp/_view/tp?startkey=1388607960000&endkey={}

此处 endkey 确实包含任何数据,但必须将 endkey 值作为 {} 传递。当我传递 endkey={} 时,出现如下错误:

   Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 97: http://localhost:5984/tp/_design/tp/_view/tp?startkey=1388607960000&endkey={}
at java.net.URI.create(URI.java:859)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:72)
at couch.TripViewExample.viewsDemo(TripViewExample.java:26)
at couch.TripViewExample.main(TripViewExample.java:18)
Caused by: java.net.URISyntaxException: Illegal character in query at index 97:

http://localhost:5984/tp/_design/tp/_view/tp?startkey=1388607960000&endkey={}
at java.net.URI$Parser.fail(URI.java:2829)
at java.net.URI$Parser.checkChars(URI.java:3002)
at java.net.URI$Parser.parseHierarchical(URI.java:3092)
at java.net.URI$Parser.parse(URI.java:3034)
at java.net.URI.<init>(URI.java:595)
at java.net.URI.create(URI.java:857)

谁能帮我解决这个错误???

最佳答案

感谢您的回复...我没有将参数与 URL 一起传递,而是按照下面的方式进行操作,现在可以正常工作了。

    HttpGet get = new HttpGet("http://localhost:5984/tp/_design/tp/_view/tp?startkey=1388607960000");
HttpParams params=new BasicHttpParams();
System.out.println(epoch);
params.setParameter("endkey", "{}");
get.setParams(params);

关于java - 如何从Java代码调用couchDB View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23602512/

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