SAP-与-JSON的接口实施1_第1页
SAP-与-JSON的接口实施1_第2页
SAP-与-JSON的接口实施1_第3页
SAP-与-JSON的接口实施1_第4页
SAP-与-JSON的接口实施1_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、SAP 与 JSON 的接口 STYLEREF Document Control Information * MERGEFORMAT 文档控制信息 DATE yyyy-MM-dd 2016-02-22第 SAP BDC 与 JSON的接口SAP 与 JSON 接口的实现方式SAP Search help简介与实现 STYLEREF Document Identification * MERGEFORMAT Error! No text of specified style in document.目录 DATE yyyy-MM-dd 2016-02-22第 Error! No text of

2、specified style in document.文档介绍 本文档旨在介绍SAP 与 JSON 接口的实现方式。什么是JSON有一种叫做JSON (JavaScript Object Notation) 的轻量级数据交换格式能够替代XML的工作. 优点: 1. 数据格式比较简单, 易于读写, 格式都是压缩的, 占用带宽小 2. 易于解析这种语言, 客户端JavaScript可以简单的通过eval()进行JSON数据的读取 3. 支持多种语言。JSON的一般调用Step1: Send the requestSend a JSON request via GET to Certipedia

3、to the relevant API end point, e.g.: HYPERLINK http:/xx.xx.0.216/api/keywords http:/xx.xx.0.216/api/keywords Note 1:The request has to be authorized via Basic access authentication See HYPERLINK /wiki/Basic_access_authentication /wiki/Basic_access_authentication.Note 2:The data are alwaysreturned JS

4、ON-encoded in the HTTP responsestructured as an Array of Hashes See HYPERLINK /wiki/Associative_array /wiki/Associative_arrayStep 2: Receive and interpret the responseExample HTTP response in which single keyword is received: key: yield-tested, deleted_at: null, translations: en: name: Yield Tested

5、, de: name: Ergiebigkeit geprft , pl: name: Przebadana wydajno Note: 可以在IE中直接输入JSON request的地址,能够看到返回结果。SAP 与 JSON 接口实施案例一 接口背景Request 无参数Receive是纯数据变量定义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 st

6、ring,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 requestlv_url

7、= http:/xx.xxx.0.217/api/keywords.CALL METHOD cl_http_client=create_by_urlEXPORTINGurl = lv_url IMPORTINGclient = lo_clientEXCEPTIONSargument_not_found = 1plugin_not_active = 2internal_error = 3OTHERS = 4.IF sy-subrc IS NOT INITIAL. ErrorEXIT.ENDIF.lo_client-request-set_header_field( EXPORTING name

8、= request_method value = GET ).lo_client-authenticate(EXPORTING* proxy_authentication = Xusername = certipediapassword = jfds8201* language =).CALL METHOD lo_client-sendEXCEPTIONShttp_communication_failure = 1http_invalid_state = 2http_processing_failed = 3http_invalid_timeout = 4OTHERS = 5.IF sy-su

9、brc 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.ENDIF.接受返回值CALL METHOD lo_client-receiveEXCEPTIONShttp_communication_failure = 1http_invalid_state = 2http_processing_failed = 3OTHERS = 4.CALL METHOD lo_client-get_last_errorIMPORTINGcode = lv_errocodeme

10、ssage = lv_message.lv_input = lo_client-response-get_cdata( ).返回数据格式转换CALL FUNCTION SCMS_STRING_TO_XSTRINGEXPORTINGtext = lv_inputIMPORTINGbuffer = lv_xinputEXCEPTIONSfailed = 1OTHERS = 2.IF sy-subrc 0.* Implement suitable error handling hereENDIF.lr_json_reader = cl_sxml_string_reader=create( input

11、 = lv_xinput ).CALL TRANSFORMATION id SOURCE XML lr_json_readerRESULT keywords = lt_result.lo_client-close( ).SAP 与 JSON 接口实施案例二接口背景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 st

12、ring. 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.处理参数 lv_parameter = & id: =ID=, & caption: =TMC=, & layout: 3, & keywords: =KEYWORD=, & qr_code_url: null, & third_

13、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( l

14、v_parameter ). CALL FUNCTION CONVERT_STRING_TO_TABLE EXPORTING i_string = lv_parameter i_tabline_length = 80 TABLES et_table = lt_table.发送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

15、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 METH

16、OD lo_client-request-set_header_field EXPORTING name = request_uri value = /generator/tdcgenerator.svc/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_

17、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.接受反馈信息 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. l

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论