Praneeth Vadlakonda
Praneeth Vadlakonda

Reputation: 37

How to call couchDB view from Java code?

I am calling couchDB view from my java code, code is like below.

I am passing my CouchDB View URL to the HttpGet() method and able to get the data.

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

But now requirement has changed want to pass one more parameter to the View URL like below :

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

Here endkey does contain any data but have to pass endkey values as {} . When I am passing endkey={} then getting error as below:

   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)

can anyone help me to fix the error???

Upvotes: 0

Views: 1754

Answers (2)

Praneeth Vadlakonda
Praneeth Vadlakonda

Reputation: 37

Thanks for ur reply...Instead of passing parameters along with URL, I have done like below, its working now.

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

Upvotes: 1

Srikanth Hugar
Srikanth Hugar

Reputation: 385

Suggest you to use libraries like jcouchDB, Jcouchdb offers many different ways of using java classes as !CouchDB documents http://code.google.com/p/jcouchdb/wiki/Tutorial

Upvotes: 0

Related Questions