跳至主要內容
Adaptive Planning
上次更新時間 :2023-06-23
用戶端 Java 程序範例

用戶端 Java 程序範例

以下呼叫「exportAccounts」API 方法的範例 Java 程序。API 方法呼叫的結果會列印到畫面上。在實際應用程式中,系統會剖析 XML 資料並處理基礎科目資料。
import java.net.HttpURLConnection; import java.net.URL; public class WebServiceClient { /** * Make an "exportAccounts" method call. */ public static void main(String[] args) throws Exception { String request = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<call method=\"exportAccounts\">\n" + " <credentials login=\"sampleuser@company.com\" password=\"my.pw\"/ callerName=\"test program\">\n" + "</call>"; // Make a URL connection to the Adaptive Planning web service URL URL url = new URL("https://api.adaptiveplanning.com/api/v14"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Set the content type, the HTTP method, and tell the connection we expect output conn.setRequestProperty("content-type", "text/xml;charset=UTF-8"); conn.setRequestMethod("POST"); conn.setDoOutput(true); // Send the request writeRequest(conn, request); // Read the response String response = readResponse(conn); // Print it out System.out.println(response); } /** * Write the request to the given URLConnection */ private static void writeRequest(HttpURLConnection conn, String request) throws Exception { conn.getOutputStream().write(request.getBytes("UTF-8")); } /** * Read the response from the given URL Connection */ private static String readResponse(HttpURLConnection conn) throws Exception { byte[] buffer = new byte[4096]; StringBuilder sb = new StringBuilder(); int amt; while ((amt = conn.getInputStream().read(buffer)) != -1) { String s = new String(buffer, 0, amt, "UTF-8"); sb.append(s); } return sb.toString(); } }