*&---------------------------------------------------------------------*
*& Report YN_API_DEMO02
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YN_API_DEMO02.
DATA: CL_SY_REF_IS_INITIAL TYPE REF TO CX_SY_REF_IS_INITIAL,
CL_XSLT_FORMAT_ERROR TYPE REF TO CX_XSLT_FORMAT_ERROR.
DATA LV_MESSAGE TYPE BAPI_MSG.
DATA LV_RETURN TYPE I.
DATA LV_URL TYPE STRING.
TYPES: BEGIN OF T_DISTRICTS4,
CODE TYPE STRING,
NAME TYPE STRING,
LEVEL TYPE I,
END OF T_DISTRICTS4.
TYPES: TT_DISTRICTS4 TYPE STANDARD TABLE OF T_DISTRICTS4 WITH DEFAULT KEY.
TYPES: BEGIN OF T_DISTRICTS3,
CODE TYPE STRING,
NAME TYPE STRING,
LEVEL TYPE I,
DISTRICTS TYPE TT_DISTRICTS4,
END OF T_DISTRICTS3.
TYPES: TT_DISTRICTS3 TYPE STANDARD TABLE OF T_DISTRICTS3 WITH DEFAULT KEY.
TYPES: BEGIN OF T_DISTRICTS2,
CODE TYPE STRING,
NAME TYPE STRING,
LEVEL TYPE I,
DISTRICTS TYPE TT_DISTRICTS3,
END OF T_DISTRICTS2.
TYPES: TT_DISTRICTS2 TYPE STANDARD TABLE OF T_DISTRICTS2 WITH DEFAULT KEY.
TYPES: BEGIN OF T_JSON1,
STATUS TYPE I,
DATA_VERSION TYPE STRING,
RESULT_SIZE TYPE I,
DISTRICTS TYPE TT_DISTRICTS2,
END OF T_JSON1.
DATA LS_RECORD TYPE T_JSON1.
DATA LV_KEYWORD TYPE STRING VALUE '440000'.
DATA LV_SUB_ADMIN TYPE STRING VALUE '2'.
DATA LV_AK TYPE STRING VALUE 'vPYH1Cdua7cBePxFFrphmdL6kfAXHdnq'.
DATA LV_STATUS TYPE STRING .
DATA LV_DATA_VERSION TYPE STRING .
DATA LV_RESULT_SIZE TYPE STRING .
START-OF-SELECTION.
LV_URL = 'http://api.map.baidu.com/api_region_search/v1/?keyword=' && LV_KEYWORD && '&sub_admin=' && LV_SUB_ADMIN && '&ak=' && LV_AK .
CONDENSE LV_URL NO-GAPS .
"根据URL生成HTTP代理
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL(
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = DATA(LO_HTTP_CLIENT)
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4 ).
*设置 HTTP 版本
LO_HTTP_CLIENT->REQUEST->SET_VERSION( IF_HTTP_REQUEST=>CO_PROTOCOL_VERSION_1_0 ).
CHECK LO_HTTP_CLIENT IS NOT INITIAL.
*将HTTP代理设置GET方法
LO_HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET ).
TRY.
"发送HTTPS请求
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2.
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
CATCH CX_SY_REF_IS_INITIAL INTO CL_SY_REF_IS_INITIAL.
LV_MESSAGE = CL_SY_REF_IS_INITIAL->IF_MESSAGE~GET_TEXT( ).
MESSAGE LV_MESSAGE TYPE 'E'.
ENDTRY.
* 获取数据
DATA(LV_JSON) = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
*关闭HTTP链接
LO_HTTP_CLIENT->CLOSE( ).
*转成大写
TRANSLATE LV_JSON TO UPPER CASE .
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_JSON
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING DATA = LS_RECORD ).
BREAK-POINT.
* DATA(OUT) = CL_DEMO_OUTPUT=>NEW( ).
* OUT->BEGIN_SECTION( `行政区划查询服务输出`
* )->WRITE( LS_RECORD ).
* OUT->DISPLAY( ).
使用/UI2/CL_JSON解析时需要先定义json对应的abap类型,这里推荐一个开源工具可以通过json直接生成abap结构,项目地址如下
https://github.com/fidley/JSON2ABAPTypehttps://github.com/fidley/JSON2ABAPType