SAP 与 JSON 接口实施案例一
DATA: lo_client TYPE REF TO if_http_client,
lo_request TYPE REF TO if_http_request,
lv_result TYPE string,
lv_errocode TYPE sysubrc,
lv_message TYPE string,
lv_input TYPE string,
lv_xinput TYPE xstring.
DATA:
lr_json_reader TYPE REF TO if_sxml_reader.
DATA: lv_url TYPE string.
TYPES: BEGIN OF lty_keywords,
key TYPE string,
deleted_at TYPE string,
translations TYPE ty_trans,
END OF lty_keywords.
DATA: lt_result TYPE TABLE OF lty_keywords.
- 调用JSON request
lv_url = 'http://xx.xxx.0.217/api/keywords'.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc IS NOT INITIAL.
" Error
EXIT.
ENDIF.
lo_client->request->set_header_field( EXPORTING name = '~request_method' value = 'GET' ).
lo_client->authenticate(
EXPORTING
* proxy_authentication = 'X'
username = 'certipedia'
password = 'jfds8201'
* language =
).
CALL METHOD lo_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL METHOD lo_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CALL METHOD lo_client->get_last_error
IMPORTING
code = lv_errocode
message = lv_message.
lv_input = lo_client->response->get_cdata( ).
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_input
IMPORTING
buffer = lv_xinput
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
lr_json_reader = cl_sxml_string_reader=>create( input = lv_xinput ).
CALL TRANSFORMATION id SOURCE XML lr_json_reader
RESULT keywords = lt_result.
lo_client->close( ).
文章来自于网络,如果侵犯了您的权益,请联系站长删除!