簡體   English   中英

如何從Java代碼調用CouchDB視圖?

[英]How to call couchDB view from Java code?

我從我的Java代碼中調用bedDB視圖,代碼如下。

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

誰能幫我解決錯誤???

感謝您的回復...我沒有像下面那樣傳遞參數,而是像現在這樣工作。

    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);

建議您使用像jcouchDB這樣的庫,Jcouchdb提供了許多將Java類用作!CouchDB文檔的方法http://code.google.com/p/jcouchdb/wiki/Tutorial

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM