我在 Eclipse 中有 java 代码,我已经完成了 eclipse 和 IBM bluemix cloudant 服务之间所需的所有设置我不知道如何更新我的代码以在 eclipse 中启用 cloudant 有人可以帮忙吗?
问问题
1122 次
3 回答
4
您需要在项目源目录下的 CloudantClient.java 文件中添加一段代码。请在 CloudantClient 类中添加这些行:
String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
JSONObject vcap;
vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
cloudant = (JSONArray) vcap.get("cloudantNoSQULDB");
cloudantInstance = (JSONObject) cloudant.get(0);
cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
您也可以将这段代码放入 try catch 循环中。
try {
String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
JSONObject vcap;
vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
cloudant = (JSONArray) vcap.get("cloudantNoSQULDB");
cloudantInstance = (JSONObject) cloudant.get(0);
cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
}
catch (IOException e) {
e.printStackTrace();
}
我希望它有效!
于 2014-12-31T10:52:06.203 回答
1
您可以使用 Bluemix 配置解析器库来自动解析 VCAP_SERVICES 环境变量 ( https://github.com/icha024/bluemix-config-parser )
它将凌乱的代码简化为...
String username = BluemixConfigStore.getConfig().getCloudantNoSQLDB()
.getCredentials().getUsername();
String password = BluemixConfigStore.getConfig().getCloudantNoSQLDB()
.getCredentials().getPassword();
然后,您可以像往常一样从中创建 Cloudant 客户端:
CloudantClient cloudantClient = ClientBuilder.account(username)
.username(username)
.password(password)
.build();
于 2016-02-01T17:46:45.220 回答
0
您需要使用 bluemix 中使用的 VCAP_SERVICES 环境变量,如下所示:
private JSONArray cloudant;
private JSONObject cloudantInstance;
private JSONObject cloudantCredentials;
public CloudantClient()
{
this.httpClient = null;
try {
String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
JSONObject vcap;
vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
cloudant = (JSONArray) vcap.get("cloudantNoSQLDB");
cloudantInstance = (JSONObject) cloudant.get(0);
cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
} catch (IOException e) {
e.printStackTrace();
}
this.port = Config.CLOUDANT_PORT;
this.host = (String) cloudantCredentials.get("host");
this.username = (String) cloudantCredentials.get("username");
this.password = (String) cloudantCredentials.get("password");
this.name = Config.CLOUDANT_NAME;
this.dbc = this.createDBConnector();
}
于 2014-12-31T12:17:25.667 回答