1

It's not clicking how one is supposed to connect to a hosted Cloudant database using Ektorp. I'm using Ektorp 1.1 in Eclipse via the new m2eclipse Maven integration (which is pretty sweet). I'm struggling to find good CouchDB/Cloudant/Ektorp documentation other than javadocs.

I'm trying to get the sample Ektorp API example from their main page to work:

HttpClient httpClient = new StdHttpClient.Builder()
                                    .host("localhost")
                                    .port(5984)
                                    .build();

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = new StdCouchDbConnector("mydatabase", dbInstance);

db.createDatabaseIfNotExists();

It doesn't matter what I use to build the httpClient with, I always get the UnknownHostException error below. I've tried these URLs for the host: https/http://cloudant.com/db/_session and https/http://[username].cloudant.com

What about the port number? Should the username and password be included in the StdHttpClient.Builder()?

Here's the full error - it's failing on the createDatabaseIfNotExists() call but I'm not confident the CouchDbConnector variable is correct.

Exception in thread "main" org.ektorp.DbAccessException: java.net.UnknownHostException: https://cloudant.com/db/_session
    at org.ektorp.util.Exceptions.propagate(Exceptions.java:19)
    at org.ektorp.http.StdHttpClient.executeRequest(StdHttpClient.java:104)
    at org.ektorp.http.StdHttpClient.get(StdHttpClient.java:42)
    at org.ektorp.http.RestTemplate.get(RestTemplate.java:21)
    at org.ektorp.impl.StdCouchDbInstance.getAllDatabases(StdCouchDbInstance.java:61)
    at org.ektorp.impl.StdCouchDbConnector.createDatabaseIfNotExists(StdCouchDbConnector.java:256)
    at com.codegouge.examples.App.main(App.java:30)
Caused by: java.net.UnknownHostException: https://cloudant.com/db/_session
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
    at java.net.InetAddress.getAllByName(InetAddress.java:1084)
    at java.net.InetAddress.getAllByName(InetAddress.java:1020)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:126)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at org.ektorp.http.StdHttpClient.executeRequest(StdHttpClient.java:96)
4

3 回答 3

3

所以我做错了几件事。使用 SSL需要额外的参数。此外, Ektorp 1.1.1 包括对 1.1.0的与 SSL 相关的错误修复。所以这是我最终的 HttpClient 构造函数:

HttpClient httpClient = new StdHttpClient.Builder()
        .host("[username].cloudant.com")
        .port(443)
        .username("[username]")
        .password("[password]")
        .enableSSL(true)
        .relaxedSSLSettings(true)
        .build();

此外,请务必在 pom.xml 中更新 ektorp 的依赖项以查找版本“1.1.1”。如果有兴趣,我有一篇关于此练习的博客文章

于 2011-04-23T21:02:13.797 回答
1

您还可以使用 URL 与 Ektorp 连接:

JSONObject serviceAttr = val.getJSONObject(0);
                        JSONObject credentials = serviceAttr.getJSONObject("credentials");
                        httpClient = new StdHttpClient.Builder()
                        .url(credentials.getString("url"))
                        .build();

这是一种简单的连接方式。我找到了使用 Ektorp 1.4.2 进行连接的教程:http: //www.ibm.com/developerworks/java/library/j-hangman-app/index.html

于 2014-09-04T18:46:04.960 回答
0

我对 Ektorp 不是很熟悉,但你肯定需要在那里输入你的用户名/密码。我建议使用以下代码创建 HttpClient:

HttpClient httpClient = new StdHttpClient.Builder()
                                    .host("[username].cloudant.com")
                                    .port(443)
                                    .username("[username]")
                                    .password("[password]")
                                    .build();

我已将端口更改为 443(Cloudant 侦听的 HTTPS 的默认端口),并添加了用户名和密码。我看不出有任何方法可以让 Ektorp 知道您想使用 HTTPS,但幸运的是,这将在内部处理。

于 2011-04-23T14:46:54.697 回答