它没有点击如何使用Ektorp连接到托管的Cloudant数据库。我通过新的m2eclipse Maven集成在Eclipse中使用Ektorp 1.1(非常好)。我很难找到除了javadocs之外的好的CouchDB / Cloudant / Ektorp文档。
我正在尝试从他们的main page获取示例Ektorp API示例:
HttpClient httpClient = new StdHttpClient.Builder()
.host("localhost")
.port(5984)
.build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = new StdCouchDbConnector("mydatabase", dbInstance);
db.createDatabaseIfNotExists();
我用什么构建httpClient并不重要,我总是得到下面的UnknownHostException错误。我已经为主机试过这些网址:https / http://cloudant.com/db/_session和https / http:// [用户名] .cloudant.com
端口号怎么样?用户名和密码是否应包含在StdHttpClient.Builder()?
中这是完整的错误 - 它在createDatabaseIfNotExists()调用失败,但我不确定CouchDbConnector变量是否正确。
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)
答案 0 :(得分:3)
所以我做错了几件事。使用SSL requires additional parameters。此外,Ektorp 1.1.1 includes SSL-related bug fixes到1.1.0。所以这是我最后的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”。如果感兴趣的话,我有一篇博文,内容涵盖了这个练习here。
答案 1 :(得分: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
答案 2 :(得分:0)
我对Ektorp不太熟悉,但你肯定需要在那里获取你的用户名/密码。我建议使用以下代码创建HttpClient:
HttpClient httpClient = new StdHttpClient.Builder()
.host("[username].cloudant.com")
.port(443)
.username("[username]")
.password("[password]")
.build();
我已将端口更改为443(Cloudant侦听的HTTPS的默认设置),并且我添加了用户名和密码。我没有看到任何方式让Ektorp知道您想要使用HTTPS,但幸运的是,这将在内部处理。