SAP 与 JSON的接口实施1

SAP 与 JSON的接口实施1


2024年5月13日发(作者:)

SAP 与 JSON 接口的实现方式

目录

1

2

3

SAP Search help简介与实现 错误!文档中没有指定

样式的文字。

文档介绍 ................................................................................................................................................. 3

什么是JSON .......................................................................................................................................... 4

JSON的一般调用 ................................................................................................................................... 5

3.1

Step1: Send the request ................................................................................................................. 5

3.2

Step 2: Receive and interpret the response ................................................................................... 5

SAP 与 JSON 接口实施案例一 .............................................................................................................. 6

4.1

接口背景 ......................................................................................................................................... 6

4.1.1

Request 无参数 ..................................................................................................................... 6

4.1.2

Receive是纯数据 .................................................................................................................. 6

4.2

变量定义 ......................................................................................................................................... 6

4.3

调用JSON request .......................................................................................................................... 6

4.4

接受返回值 ...................................................................................................................................... 7

4.5

返回数据格式转换 ........................................................................................................................... 7

SAP 与 JSON 接口实施案例二 .............................................................................................................. 8

5.1

接口背景 ......................................................................................................................................... 8

5.1.1

Request 有传入参数 .............................................................................................................. 8

5.1.2

Receive是返回一个jpg的URL ............................................................................................ 8

5.2

数据定义 ......................................................................................................................................... 8

5.3

处理参数 ......................................................................................................................................... 8

5.4

发送JSON request .......................................................................................................................... 9

5.5

接受反馈信息 .................................................................................................................................. 9

5.6

处理URL ........................................................................................................................................ 10

4

5

目录

2022-04-25

第II页 错误!文档中没有指定样式的文字。

1 文档介绍

本文档旨在介绍SAP 与 JSON 接口的实现方式。

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第3 页 错误!文档中没有指定样式的文字。

2 什么是JSON

有一种叫做JSON (JavaScript Object Notation) 的轻量级数据交换格式能够替代XML的工作. 优点: 1. 数据格式比

较简单, 易于读写, 格式都是压缩的, 占用带宽小 2. 易于解析这种语言, 客户端JavaScript可以简单的通过eval()

进行JSON数据的读取 3. 支持多种语言。

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第4 页 错误!文档中没有指定样式的文字。

3 JSON的一般调用

3.1 Step1: Send the request

Send a JSON request via GET to Certipedia to the relevant API end point, e.g.:

.0.216/api/keywords

Note 1: The request has to be authorized via Basic access authentication

1

.

Note 2: The data are always

• returned JSON-encoded in the HTTP response

• structured as an Array of Hashes

2

3.2 Step 2: Receive and interpret the response

Example HTTP response in which single keyword is received:

[

{

"key": "yield-tested",

"deleted_at": null,

"translations": {

"en": {

"name": "Yield Tested"

},

"de": {

"name": "Ergiebigkeit

geprüft"

},

"pl": {

"name": "Przebadana wydajność"

}

}

}

]

Note: 可以在IE中直接输入JSON request的地址,能够看到返回结果。

1

See /wiki/Basic_access_authentication

2

See /wiki/Associative_array

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第5 页 错误!文档中没有指定样式的文字。

4 SAP 与 JSON 接口实施案例一

4.1

4.1.1

4.1.2

4.2 变量定义

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.

接口背景

Request 无参数

Receive是纯数据

4.3 调用JSON request

lv_url = '.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

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第6 页 错误!文档中没有指定样式的文字。

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.

4.4 接受返回值

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( ).

4.5 返回数据格式转换

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( ).

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第7 页 错误!文档中没有指定样式的文字。

5 SAP 与 JSON 接口实施案例二

5.1

5.1.1

5.1.2

5.2

接口背景

Request 有传入参数

Receive是返回一个jpg的URL

数据定义

DATA: lo_client TYPE REF TO if_http_client,

lo_request TYPE REF TO if_http_request,

lv_parameter TYPE string,

lv_parameter2 TYPE string.

DATA: lv_result TYPE string,

lv_errocode TYPE sysubrc,

lv_message TYPE string.

DATA: ls_config TYPE zwzert_id_inf,

lv_username TYPE string,

lv_psw TYPE string,

lv_url TYPE string.

5.3 处理参数

lv_parameter = ' { '

& '"id": "=ID=",'

& '"caption": =TMC=, '

& '"layout": 3,'

& '"keywords": [ =KEYWORD=],'

& '"qr_code_url": null,'

& ' "third_party_mark": null,'

& ' "annual_ring_range": null,'

& ' "annual_ring_text": null,'.

lv_parameter2 =

' "output_format": 1,'

& ' "output_quality": 1,'

* & ' "generate_zip": true,'

& ' "generate_zip": false,'

& ' "use_small_font": =FONT=,'

& ' "langs": ['

& ' "=LANG=",'

& ' "=LANG=",'

& '"=LANG="'

* & ' ]'

& ' ],'

& '"generate_jpg_instead_png":true'

& '}'.

lv_length = strlen( lv_parameter ).

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第8 页 错误!文档中没有指定样式的文字。

CALL FUNCTION 'CONVERT_STRING_TO_TABLE'

EXPORTING

i_string = lv_parameter

i_tabline_length = 80

TABLES

et_table = lt_table.

5.4 发送JSON request

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.

lo_client->request->set_header_field( EXPORTING name = '~request_method' value = 'POST' ).

lo_client->request->set_header_field( EXPORTING name = '~request_protocol' value = 'HTTP/1.0' ).

lo_client->request->set_header_field( EXPORTING name = 'Content-Type' value = 'text/xml' ).

CALL METHOD lo_client->request->set_header_field

EXPORTING

name = '~request_uri'

value = '/generator//generator'.

lo_client->request->set_cdata( EXPORTING data = lv_parameter offset = 0 length = lv_length ).

CALL METHOD lo_client->authenticate

EXPORTING

* proxy_authentication = 'X'

* client =

username = lv_username

password = lv_psw

* 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.

5.5 接受反馈信息

CALL METHOD lo_client->receive

EXCEPTIONS

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第9 页 错误!文档中没有指定样式的文字。

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_result = lo_client->response->get_cdata( ).

5.6

*JPG

处理URL

FIND REGEX 'http.*jpg' IN lv_result

MATCH OFFSET lv_moff

MATCH LENGTH lv_mlen.

TRY .

ev_url = substring( val = lv_result off = lv_moff len = lv_mlen ).

CATCH cx_sy_range_out_of_bounds.

MESSAGE e151(zw).

ENDTRY.

lo_client->close( ).

错误!使用“开始”选项卡将 Heading 1 应用于要

在此处显示的文字。

2022-04-25

第10 页 错误!文档中没有指定样式的文字。


发布者:admin,转转请注明出处:http://www.yc00.com/news/1715537065a2633159.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信