我正在研究IBM的快速文件API。現在我是前端開發人員,對Java不太瞭解。實現Java API示例代碼
我想知道我怎麼能實現一些他們的developers website提供的示例代碼:這裏是樣本代碼之一:
下面是如何使用的例子獲取用戶列表的API。此Java程序獲取使用REST請求的用戶列表,將結果解析爲正則表達式,然後將總用戶數打印到控制檯。
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
public class QuickFileUserCount {
public static void main(String[] args) {
try {
if (args.length < 4) {
System.out.println("Usage is java QuickFileUserCount <host> <port> <userid> <password>");
System.exit(0);
}
String quickFileServer = args[0];
String quickFilePort = args[1];
String quickFileUser = args[2];
String quickFilePassword = args[3];
String quickFileUserQuery = "http://" + quickFileServer + ":" + quickFilePort + "/quickfile/rest/admin/users/0/0";
URL url = new URL(quickFileUserQuery);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestMethod("GET");
uc.setDoInput(true);
String uidpw = quickFileUser + ":" + quickFilePassword;
String encodedPassword = Base64.encodeBase64String(uidpw.getBytes());
uc.setRequestProperty("Authorization", "Basic " + encodedPassword);
InputStream is = uc.getInputStream();
StringBuffer sb = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
sb.append((char) c);
}
String ss = sb.toString();
String ps = "(.*?)\"totalRows\":(.+?),.*";
String userCount = ss.replaceAll(ps, "$2");
System.out.println("\nNumber of QuickFile users on server <" + quickFileServer + "> = " + userCount);
uc.disconnect();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
我需要建立一個定製的APP來看到這個動作?如果是這樣,我該如何解決這個問題?由於我是編程新手,從哪裏開始?
我將不勝感激任何答案
也許會是最容易從命令行運行你可以看看http://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command -line for reference – chancea
非常感謝您的鏈接 –