java调用SAP RFC函数

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jcoapp;

import com.sap.mw.jco.*;
import com.sap.conn.jco.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author luolai
 */
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Table;
import java.util.Vector;

public class JCOClientDemo {

    static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
    static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
    static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";
    static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";
//    private String JCO_ASHOST = "";
//    private String JCO_SYSNR = "";
//    private String JCO_CLIENT = "";
//    private String JCO_USER = "";
//    private String JCO_PASSWD = "";
//    private String JCO_LANG = "";
    LogInfo loginfo = new LogInfo();

    public JCOClientDemo(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD,
            String JCO_LANG) {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, JCO_ASHOST);
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, JCO_SYSNR);
//		connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, "C01");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, JCO_CLIENT);
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, JCO_USER);
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, JCO_PASSWD);
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, JCO_LANG);
//        createDataFile(ABAP_AS, "jcoDestination", connectProperties);

        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "5");
        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
    }

    static void createDataFile(String name, String suffix, Properties properties) {
        File cfg = new File(name + "." + suffix);
        if (cfg.exists()) {
            cfg.delete();
        }
        if (!cfg.exists()) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(cfg, false);
                properties.store(fos, "for tests only !");
                fos.close();
            } catch (Exception e) {
                throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static String callFuction(String strF, String echo) throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        JCoFunction function = destination.getRepository().getFunction(strF);
        if (function == null) {
            throw new RuntimeException(strF + " not found in SAP.");
        }
        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return "Error";
        }

        //System.out.println(" Echo: " + function.getExportParameterList().toXML());
        //System.out.println(" Echo: " + function.getExportParameterList().getString(echo));
        return function.getExportParameterList().getString(echo);
    }

    public static void call(String thread) throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");
        if (function == null) {
            throw new RuntimeException("STFC_CONNECTION not found in SAP.");
        }

        function.getImportParameterList().setValue("REQUTEXT", "Hello SAP");

        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }
        System.out.println("thread:" + thread);
        System.out.println("STFC_CONNECTION finished:");
        System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));
        System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));
        System.out.println();
    }

    public static void step1Connect() throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        System.out.println("Attributes:");
        System.out.println(destination.getAttributes());
        System.out.println();
    }

    public static void step2ConnectUsingPool() throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        destination.ping();
        System.out.println("Attributes:");
        System.out.println(destination.getAttributes());
        System.out.println();
    }

    public static void step3SimpleCall() throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");
        if (function == null) {
            throw new RuntimeException("STFC_CONNECTION not found in SAP.");
        }
        function.getImportParameterList().setValue("REQUTEXT", "Hello SAP");
        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }
        System.out.println("STFC_CONNECTION finished:");
        System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));
        System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));
        System.out.println();
    }

    public static void step3WorkWithStructure() throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        JCoFunction function = destination.getRepository().getFunction("RFC_SYSTEM_INFO");
        if (function == null) {
            throw new RuntimeException("RFC_SYSTEM_INFO not found in SAP.");
        }
        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }
        JCoStructure exportStructure = function.getExportParameterList().getStructure("RFCSI_EXPORT");
        System.out.println("System info for " + destination.getAttributes().getSystemID() + ":\n");
        for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {
            System.out.println(exportStructure.getMetaData().getName(i) + ":\t" + exportStructure.getString(i));
        }
        System.out.println();
//JCo still supports the JCoFields, but direct access via getXX is more efficient as field iterator
        System.out.println("The same using field iterator: \nSystem info for " + destination.getAttributes().getSystemID() + ":\n");
        for (JCoField field : exportStructure) {
            System.out.println(field.getName() + ":\t" + field.getString());
        }
        System.out.println();
    }

    public static void step4WorkWithTable() throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
        JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
        if (function == null) {
            throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
        }
        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }
        JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");
        if (!(returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S"))) {
            throw new RuntimeException(returnStructure.getString("MESSAGE"));
        }
        System.out.println("COMP_CODE" + '\t' + "COMP_NAME");
        JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");
        for (int i = 0; i < codes.getNumRows(); i++) {
            codes.setRow(i);
            System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));
        }

        codes.firstRow();
        System.out.println("COMP_CODE" + '\t' + "COUNTRY" + '\t' + "CITY");
        for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) {
            function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");
            if (function == null) {
                throw new RuntimeException("BAPI_COMPANYCODE_GETDETAIL not found in SAP.");
            }
            function.getImportParameterList().setValue("COMPANYCODEID", codes.getString("COMP_CODE"));
            function.getExportParameterList().setActive("COMPANYCODE_ADDRESS", false);
            try {
                function.execute(destination);
            } catch (AbapException e) {
                System.out.println(e.toString());
                return;
            }
            returnStructure = function.getExportParameterList().getStructure("RETURN");
            if (!(returnStructure.getString("TYPE").equals("")
                    || returnStructure.getString("TYPE").equals("S")
                    || returnStructure.getString("TYPE").equals("W"))) {
                throw new RuntimeException(returnStructure.getString("MESSAGE"));
            }
            JCoStructure detail = function.getExportParameterList().getStructure("COMPANYCODE_DETAIL");
            System.out.println(detail.getString("COMP_CODE") + '\t'
                    + detail.getString("COUNTRY") + '\t'
                    + detail.getString("CITY"));
        }//for
    }

    public static void main(String[] args) {
        // Print the version of the underlying JCO library
        System.out.println("\nVersion of the JCO-library:\n" + "---------------------------\n" + JCO.getMiddlewareVersion());
        try {
            System.out.println(JCOClientDemo.callFuction("ZWMF_GET_FUNCTION", "ITAB"));
            //JCOClientDemo.call("");
            //JCOClientDemo.step4WorkWithTable();
        } catch (JCoException e) {
            e.printStackTrace();
        }
    }
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Android Studio中远程调用SAP RFC函数,可以通过以下步骤实现: 1. 首先,确保你的Android Studio项目中已经添加了相应的SAP Java Connector(SAP JCo)库。你可以在项目的build.gradle文件中添加依赖项,例如: ```java dependencies { implementation 'com.sap.conn.jco:jco3:3.1.2' } ``` 2. 创建一个SAP连接: 在你的代码中,实例化一个`JCoDestination`对象,并设置连接所需的属性,如SAP服务器的地址、用户名、密码等。例如: ```java JCoDestination destination = JCoDestinationManager.getDestination("MY_DESTINATION"); destination.getRepository(); // 设置连接属性 destination.getProperties().setProperty(DestinationDataProvider.JCO_ASHOST, "SAP服务器地址"); destination.getProperties().setProperty(DestinationDataProvider.JCO_SYSNR, "系统编号"); destination.getProperties().setProperty(DestinationDataProvider.JCO_CLIENT, "客户端"); destination.getProperties().setProperty(DestinationDataProvider.JCO_USER, "用户名"); destination.getProperties().setProperty(DestinationDataProvider.JCO_PASSWD, "密码"); ``` 3. 调用RFC函数: 使用SAP连接后,可以从SAP系统的函数库中获取function module,然后通过函数的`execute()`方法调用RFC函数并传递参数。例如: ```java JCoFunction function = destination.getRepository().getFunction("RFC_FUNCTION_NAME"); if (function == null) { throw new RuntimeException("Function not found"); } // 设置RFC函数的输入参数 function.getImportParameterList().setValue("parameterName", parameterValue); function.execute(destination); // 获取RFC函数的输出参数 String result = function.getExportParameterList().getString("outputParamName"); ``` 4. 处理RFC函数的返回结果: 根据你的需求,可以根据RFC函数的返回结果进行一系列的操作,例如显示在界面上、保存到本地等。 以上就是在Android Studio中远程调用SAP RFC函数的基本步骤。请注意,在实际应用中,你可能还需要处理连接的异常、异常情况下的处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值