2014-12-31 49 views

回答

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 { 
       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(); 
      } 

我希望它的作品!

+0

Thanks Diwesh ... It works –

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

然後你可以創建一個C像往常一樣響亮的客戶端:

CloudantClient cloudantClient = ClientBuilder.account(username) 
            .username(username) 
            .password(password) 
            .build(); 
相關問題