【轉載】SAP中用json資料格式呼叫http介面傳送簡訊郵件案例

hubaichun發表於2018-10-10

使用JSON 資料傳輸的可以參考:

在系統 VL02N 交貨過賬後觸發傳送郵件、簡訊的功能,簡訊郵件介面是經過第三方系統封裝,然後開發出的API介面。

請求地址: http://180.167.01.01/ns/sends
請求方式: POST
引數格式: JSON
請求引數: 

  1. {
  2.     "ncode": "sapOrderSend",
  3.     "source": "訂單號",
  4.     "contacts": [
  5.         {
  6.             "code": "訂貨人郵箱地址",
  7.             "type": "1"
  8.         },
  9.         {
  10.             "code": "訂貨人手機號",
  11.             "type": "2"
  12.         }
  13.     ],
  14.     "params": {
  15.         "contactName": "訂貨人姓名",
  16.         "contactCode": "訂貨人編號",
  17.         "code": "訂單號",
  18.         "createDate": "訂單建立時間",
  19.         "storageName": "發貨倉庫名稱",
  20.         "shipTypeName": "發貨方式名稱"
  21.     }
  22. }

複製程式碼



RFC介面程式碼:

  1. FUNCTION zot_do_send_message.
  2. *"----------------------------------------------------------------------
  3. *"*"本地介面:
  4. *"  IMPORTING
  5. *"     VALUE(I_VBELN) TYPE  VBELN_VL OPTIONAL
  6. *"  EXCEPTIONS
  7. *"      URL_ERROR
  8. *"----------------------------------------------------------------------
  9.   DATA: lr_http_client TYPE REF TO if_http_client .
  10.   DATA: lv_url TYPE string,
  11.         lv_result TYPE string,
  12.         lv_post_string TYPE string,
  13.         lv_len TYPE i.
  14.  
  15.   TYPES:BEGIN OF ty_contacts,
  16.           code TYPE string,
  17.           type TYPE char1,
  18.        END OF ty_contacts.
  19.   TYPES:BEGIN OF ty_params,
  20.           contactname  TYPE string,
  21.           contactcode  TYPE string,
  22.           code         TYPE string,
  23.           createdate   TYPE string,
  24.           storagename  TYPE string,
  25.           shiptypename TYPE string,
  26.       END OF ty_params.
  27.   DATA:lt_contacts TYPE TABLE OF ty_contacts.
  28.   TYPES:BEGIN OF ty_message,
  29.           ncode TYPE string,
  30.           source TYPE string,
  31.           contacts LIKE lt_contacts,
  32.           params TYPE ty_params,
  33.         END OF ty_message.
  34.   DATA:ls_message  TYPE ty_message.
  35.   DATA:ls_contacts TYPE ty_contacts.
  36.   DATA:BEGIN OF lt_vbap OCCURS 0,
  37.         vbeln TYPE vbap-vbeln,
  38.         posnr TYPE vbap-posnr,
  39.         ktgrm TYPE vbap-ktgrm,
  40.       END OF lt_vbap.
  41.  
  42.   DATA:lv_vbeln_so   TYPE vbak-vbeln,
  43.        lv_tel_number TYPE adr2-tel_number,
  44.        lv_smtp_addr  TYPE adr6-smtp_addr,
  45.        lv_kunnr      TYPE kna1-kunnr,
  46.        lv_name       TYPE kna1-name1,
  47.        lv_erdat TYPE vbak-erdat,
  48.        lv_bname TYPE vbak-bname,
  49.        lv_bezei TYPE t173t-bezei,
  50.        lv_lgobe TYPE t001l-lgobe.
  51.  
  52.   IF i_vbeln IS INITIAL.
  53.     EXIT.
  54.   ENDIF.
  55.  
  56.   SELECT SINGLE url INTO lv_url FROM zot_message WHERE fm_name = 'ZOT_DO_SEND_MESSAGE' AND pm1 = 'URL'.
  57.   IF sy-subrc NE 0.
  58.     MESSAGE '請在表ZOT_MESSAGE配置URL引數' TYPE 'E' RAISING url_error.
  59.   ENDIF.
  60.  
  61.   SELECT vbap~vbeln
  62.          vbap~posnr
  63.          vbap~ktgrm
  64.     INTO TABLE lt_vbap
  65.     FROM lips
  66.     INNER JOIN vbap ON lips~vgbel = vbap~vbeln AND vbap~abgru = ''
  67.     INNER JOIN vbak ON vbap~vbeln = vbak~vbeln AND vbak~vkorg = '3110'  "國內發簡訊
  68.     WHERE lips~vbeln = i_vbeln.
  69.   DELETE lt_vbap WHERE ktgrm = '02'.  "技術服務
  70.   IF lt_vbap[] IS INITIAL.
  71.     EXIT.
  72.   ENDIF.
  73.  
  74.   SELECT SINGLE lips~vgbel adr2~tel_number adr6~smtp_addr kna1~kunnr kna1~name1
  75.   INTO (lv_vbeln_so,lv_tel_number,lv_smtp_addr,lv_kunnr,lv_name)
  76.   FROM lips
  77.   INNER JOIN vbpa ON lips~vgbel = vbpa~vbeln AND vbpa~posnr = space AND vbpa~parvw = 'WE'
  78.   INNER JOIN kna1 ON vbpa~kunnr = kna1~kunnr
  79.   LEFT JOIN adr2 ON kna1~adrnr = adr2~addrnumber AND adr2~persnumber = space AND adr2~r3_user = '3'
  80.   LEFT JOIN adr6 ON kna1~adrnr = adr6~addrnumber AND adr6~persnumber = space
  81.   WHERE lips~vbeln = i_vbeln.
  82.  
  83.   IF lv_tel_number IS INITIAL AND lv_smtp_addr IS INITIAL.
  84.     EXIT.
  85.   ENDIF.
  86.  
  87.   SELECT SINGLE
  88.   likp~erdat
  89.   vbak~bname
  90.   t173t~bezei
  91.   t001l~lgobe
  92.   INTO (lv_erdat,lv_bname,lv_bezei,lv_lgobe)
  93.   FROM likp
  94.   INNER JOIN lips ON likp~vbeln = lips~vbeln
  95.   INNER JOIN vbak ON lips~vgbel = vbak~vbeln
  96.   LEFT JOIN t173t ON t173t~spras = sy-langu AND t173t~vsart = likp~vsart
  97.   LEFT JOIN t001l ON lips~werks = t001l~werks AND lips~lgort = t001l~lgort
  98.   WHERE likp~vbeln = i_vbeln.
  99.  
  100. *-->資料初始化成巢狀內表形式
  101.   ls_message-ncode = 'sapOrderSend'.
  102.   IF lv_bname IS NOT INITIAL.
  103.     ls_message-source = lv_bname.
  104.   ELSE.
  105.     ls_message-source = i_vbeln.
  106.   ENDIF.
  107.   IF lv_smtp_addr IS NOT INITIAL.
  108.     ls_contacts-code = lv_smtp_addr.
  109.     ls_contacts-type = '1'.
  110.     APPEND ls_contacts TO ls_message-contacts.
  111.   ENDIF.
  112.   IF lv_tel_number IS NOT INITIAL.
  113.     ls_contacts-code = lv_tel_number.
  114.     ls_contacts-type = '2'.
  115.     APPEND ls_contacts TO ls_message-contacts.
  116.   ENDIF.
  117.  
  118.   ls_message-params-contactname = lv_name.
  119.   ls_message-params-contactcode = lv_kunnr.
  120.   IF lv_bname IS NOT INITIAL.
  121.     ls_message-params-code = lv_bname.
  122.   ELSE.
  123.     ls_message-params-code = lv_vbeln_so.
  124.   ENDIF.
  125.  
  126.   IF lv_erdat IS NOT INITIAL.
  127.     CONCATENATE lv_erdat+0(4) lv_erdat+4(2) lv_erdat+6(2) INTO ls_message-params-createdate SEPARATED BY '-'.
  128.   ENDIF.
  129.   ls_message-params-storagename = lv_lgobe.
  130.   ls_message-params-shiptypename = lv_bezei.
  131.  
  132. *-->內表資料轉-->json格式
  133.   lv_post_string = /ui2/cl_json=>serialize( data = ls_message compress = ABAP_true pretty_name = abap_true ).
  134.  
  135.   REPLACE ALL OCCURRENCES OF 'contactname'  IN lv_post_string WITH 'contactName'.
  136.   REPLACE ALL OCCURRENCES OF 'contactcode'  IN lv_post_string WITH 'contactCode'.
  137.   REPLACE ALL OCCURRENCES OF 'createdate'   IN lv_post_string WITH 'createDate'.
  138.   REPLACE ALL OCCURRENCES OF 'storagename'  IN lv_post_string WITH 'storageName'.
  139.   REPLACE ALL OCCURRENCES OF 'shiptypename' IN lv_post_string WITH 'shipTypeName'.
  140.  
  141. *  lv_len = strlen( lv_post_string ) .
  142.   lv_len = cl_abap_list_utilities=>dynamic_output_length( lv_post_string ).
  143.  
  144. *send
  145. *  lv_url = 'http://192.168.01.01:8088/ns/sends'.
  146.   CALL METHOD cl_http_client=>create_by_url
  147.     EXPORTING
  148.       url                = lv_url
  149.     IMPORTING
  150.       client             = lr_http_client
  151.     EXCEPTIONS
  152.       argument_not_found = 1
  153.       plugin_not_active  = 2
  154.       internal_error     = 3
  155.       OTHERS             = 4.
  156.  
  157.   CALL METHOD lr_http_client->request->set_header_field
  158.     EXPORTING
  159.       name  = '~request_method'
  160.       value = 'POST'.
  161.  
  162.   CALL METHOD lr_http_client->request->set_header_field
  163.     EXPORTING
  164.       name  = '~server_protocol'
  165.       value = 'HTTP/1.1'.
  166.  
  167.   CALL METHOD lr_http_client->request->set_header_field
  168.     EXPORTING
  169.       name  = 'Content-Type'
  170.       value = 'application/json; charset=utf-8'.
  171.   CALL METHOD lr_http_client->request->set_header_field
  172.     EXPORTING
  173.       name  = 'Accept'
  174.       value = 'application/json'.
  175. *  CALL METHOD lr_http_client->request->set_header_field
  176. *    EXPORTING
  177. *      name  = 'Content-Length'
  178. *      value = len_str.
  179.  
  180.  
  181.   CALL METHOD lr_http_client->request->set_cdata
  182.     EXPORTING
  183.       data   = lv_post_string
  184.       offset = 0
  185.       length = lv_len.
  186.  
  187.   CALL METHOD lr_http_client->send
  188.     EXCEPTIONS
  189.       http_communication_failure = 1
  190.       http_invalid_state         = 2.
  191.   CALL METHOD lr_http_client->receive
  192.     EXCEPTIONS
  193.       http_communication_failure = 1
  194.       http_invalid_state         = 2
  195.       http_processing_failed     = 3.
  196.  
  197.   lv_result = lr_http_client->response->get_cdata( ).
  198.  
  199. ENDFUNCTION.

相關文章