如何从Java代码调用couchDB视图?

时间:2014-05-12 06:38:39

标签: java couchdb

我从我的java代码调用couchDB视图,代码如下所示。

我将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)

任何人都可以帮我修复错误???

2 个答案:

答案 0 :(得分:1)

感谢您的回复...我没有将参数与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);

答案 1 :(得分:0)

建议你使用像jcouchDB这样的库,Jcouchdb提供了许多不同的使用java类的方法!CouchDB文档http://code.google.com/p/jcouchdb/wiki/Tutorial