如何使用REST api连接android中的Clusterpoint数据库?

时间:2015-07-12 19:27:10

标签: android database rest clusterpoint nosql

我正在使用Java构建一个Android应用程序。如何使用REST连接到Clusterpoint?我需要一些例子。

1 个答案:

答案 0 :(得分:1)

您应该设置基本授权。这是一个例子:

URL url = new URL("https://api-eu.clusterpoint.com/ACCOUNT_ID/DATABASE/_ID");

HttpURLConnection uc = (HttpURLConnection) url.openConnection();
String userpass = "USERNAME" + ":" + "PASSWORD";
String basicAuth = "Basic "
            + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass
            .getBytes());

uc.setRequestProperty("Authorization", basicAuth);

// Receive response
InputStream in = uc.getInputStream();
InputStreamReader isr = new InputStreamReader(in);

int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
        sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
System.out.println(result);

您还可以在此处找到更多代码示例:http://docs.clusterpoint.com/examples/