Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: 增加说明

这里仅以BusinessViewService的API作为示例,其它Service的API的操作方式是类似的。

这里仅以BusinessViewService的API查询数据集数据作为示例,其它Service的API的操作方式是类似的。

数据集位置:根目录\功能演示\查询定义\原生SQL查询\带参数的原生SQL

 

Code Block
titleJavaJsonAPITest.java
themeEclipse
linenumberstrue
languagejava
import java.util.List;
import smartbi.net.sf.json.*;
import smartbi.sdk.ClientConnector;
import smartbi.sdk.InvokeResult;
import smartbi.sdk.RemoteException;
public class JavaJsonAPITest {
 //打印字段信息
 private static void printFieldNames(JSONArray fNames) {
  List<String> names = (List<String>) JSONArray.toList(fNames);
  for (String fieldName : names)
   System.out.print(fieldName + ", ");
 }
 //打印结果集信息
 private static void printMatrix(JSONArray data) {
  List<List<String>> rows = JSONArray.toList(data);
  for (List<String> row : rows) {
   System.out.println();
   for (String cell : row) {
    System.out.print(cell + ", ");
   }
  }
 }
 public static void main(String[] args) {
  String Smartbi_URL = "http://localhost:8080/smartbi";
  ClientConnector conn = new ClientConnector(Smartbi_URL);
  try {
   //第一次调用必须建立一个连接,后续调用则不必再建连接
   boolean loginSuceeded = conn.open("admin", "manager");
   if (loginSuceeded) {
    // 业务查询ID(对应示例库中的“根目录\功能演示\查询定义\原生SQL查询\带参数的原生SQL”)
    String bizViewId = "I2c94907127855da201278df551b62602";
    // 参数ID
    String paramId = "OutputParameter.I2c94907127855da201278df551b62602.产品名称参数_列表对话框";
    // 参数真实值
    String paramValue = "牛奶,花生,饼干,猪肉,汽水";
    JSONObject param = new JSONObject();
    param.put("id", paramId);
    param.put("value", paramValue);
    JSONArray params = new JSONArray();
    params.put(param);
    String paramsJsonArrStr = params.toString();
    //每页行数
    int rowsPerPage = 2;
    // 是否获取总行数
    boolean getTotalRows = true;
    InvokeResult result = conn.remoteInvoke("BusinessViewService","openLoadDataView", 
						new Object[] { bizViewId,paramsJsonArrStr, rowsPerPage, getTotalRows });
    if (result.isSucceed()) {
     JSONObject ret = (JSONObject) result.getResult();
     String loadDataClientId = ret.getString("clientId");
     int totalRows = ret.getInt("totalRowCount");
     printFieldNames(ret.getJSONArray("fieldNames"));
     int pages = totalRows / rowsPerPage + (totalRows % rowsPerPage == 0 ? 0 : 1);
     for (int pageNum = 0; pageNum < pages; pageNum++) {
      InvokeResult viewData = conn.remoteInvoke("BusinessViewService", 
						"loadViewData",  
						new Object[] { loadDataClientId, pageNum });
      JSONArray pageData = (JSONArray) viewData.getResult();
      printMatrix(pageData);
     }
     conn.remoteInvoke("BusinessViewService", "closeLoadDataView", new Object[] { loadDataClientId });
    }
   }
  } catch (RemoteException e) {
   e.printStackTrace();
  } finally {
   // 关闭应用连接器
   conn.close();
  }
 }
}

...