今天是个娱乐的节日,学点新东西,如下:
突发奇想想要使用abap去获取个网站上公布的api,然后显示一下
就当最近学习NEPTUNE的小练习吧
百度一下获取疫情的网站API,也可以自己去tianapi申请,
METHOD init.
url = 'http://api.tianapi.com/txapi/ncovabroad/index?key=****************************'.
"创建http客户端
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
"设置http method 为Get
* 设定传输请求内容格式以及编码格式
http_client->request->set_content_type(
content_type = 'application/json; charset=utf-8' ).
* 设定调用服务
http_client->request->set_method(
if_http_request=>co_request_method_get ).
"发送
CALL METHOD http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
"接收
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
"获取接口返回的数据
DATA(json_result) = http_client->response->get_cdata( ).
DATA lr_data TYPE REF TO data.
DATA lr_data_t TYPE REF TO data."table
FIELD-SYMBOLS:
<table> TYPE STANDARD TABLE,
<result> TYPE STANDARD TABLE,
<data> TYPE data,
<struct> TYPE any,
<field> TYPE any.
DATA lr_struct TYPE REF TO cl_abap_structdescr.
DATA lt_comps_wa TYPE abap_compdescr_tab.
"通过/UI2/CL_JSON=>generate() 来通过JSON来转化出内表
lr_data = /ui2/cl_json=>generate( json = json_result ).
ASSIGN lr_data->* TO <data>.
ASSIGN COMPONENT 'CODE' OF STRUCTURE <data> TO FIELD-SYMBOL(<code_ref>).
ASSIGN <code_ref>->* TO FIELD-SYMBOL(<code>).
IF <code> NE 200."调用api消息失败,
"输出消息
CLEAR wa_message.
wa_message-type = 'E'.
wa_message-id = 'ZJXZHU'.
wa_message-number = 003.
wa_message-message_v1 = <code>.
ASSIGN COMPONENT 'MSG' OF STRUCTURE <data> TO FIELD-SYMBOL(<msg_ref>).
ASSIGN <msg_ref>->* TO FIELD-SYMBOL(<msg>).
wa_message-message_v2 = <msg>.
CALL METHOD server->api_message_create
EXPORTING
message = wa_message.
RETURN.
ENDIF.
*转化 , code = 200 表示接收成功
ASSIGN COMPONENT 'NEWSLIST' OF STRUCTURE <data> TO FIELD-SYMBOL(<list>).
IF sy-subrc EQ 0.
lr_data = <list>.
ASSIGN lr_data->* TO <table>.
READ TABLE <table> INTO lr_data INDEX 1.
IF sy-subrc EQ 0.
LOOP AT <table> ASSIGNING FIELD-SYMBOL(<table_line>).
ASSIGN <table_line>->* TO <data>.
APPEND INITIAL LINE TO gt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
"获取typeof
lr_struct ?= cl_abap_typedescr=>describe_by_data( <data> ).
lt_comps_wa = lr_struct->components.
LOOP AT lt_comps_wa INTO DATA(ls_comps_wa).
ASSIGN COMPONENT ls_comps_wa-name OF STRUCTURE <fs_data> TO <field>.
IF sy-subrc EQ 0.
ASSIGN COMPONENT ls_comps_wa-name OF STRUCTURE <data> TO FIELD-SYMBOL(<value_ref>).
IF sy-subrc EQ 0.
ASSIGN <value_ref>->* TO FIELD-SYMBOL(<value>).
IF sy-subrc EQ 0.
<field> = <value>.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDLOOP.
ENDIF.
ENDIF.
ENDMETHOD.
在调用这个类执行中可以发现:LR_DATA->CODE->* = 200
LR_DATA->MSG->* = success
对于NEWSLIST每一行都是一个结构对象
双击进入,可以看到获取了美国新冠疫情的最新数据,每一行都是一个国家
按照上面的结构,我们可以创建全局变量GT_DATA
DATA:
BEGIN OF gs_data,
confirmedcount TYPE i, "确诊人数
confirmedcountrank TYPE i, "确诊人数排名
continents TYPE string, "大洲
countryshortcode TYPE string, "国家代码
curedcount TYPE i, "治愈人数
currentconfirmedcount TYPE i, "当前确诊人数
deadcount TYPE i, "死亡人数
deadcountrank TYPE i, "死亡人数排名
deadrate TYPE string, "死亡率
deadraterank TYPE i, "死亡率排名
locationid TYPE i, "地址位置
modifytime TYPE i, "更新时间
provincename TYPE string, "国家名称
END OF gs_data .
DATA:
gt_data LIKE TABLE OF gs_data .
将获取到的数据显示在手机/平板端:
可以点击右上方的图标,获取累计确诊排名前20名的国家柱状图:
可以看到我们查询的时候,米国的确诊人数已经到达了
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa> TYPE ANY,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
it_structure TYPE lvc_t_fcat,
wa_structure TYPE lvc_s_fcat.
DATA gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA ls_fieldcat LIKE LINE OF gt_fieldcat.
DATA:http_client TYPE REF TO if_http_client,
url TYPE string.
DATA lr_data TYPE REF TO data.
FIELD-SYMBOLS <data> TYPE data.
FIELD-SYMBOLS <l_strut> TYPE data.
DATA lr_struct TYPE REF TO cl_abap_structdescr.
DATA lt_comps_wa TYPE abap_compdescr_tab.
START-OF-SELECTION.
PERFORM get_data.
PERFORM create_structure. "定义内表的结构
PERFORM create_dynamic_table. "按照定义的内表结构,产生一个内表
PERFORM write_data_to_dyntable. "向动态内表中写数
PERFORM show_alv_data. "显示数据
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
FORM get_data.
url = 'http://api.k780.com/?app=weather.today&weaId=2&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json'. "网上找的免费API获取北京天气预报
"创建http客户端
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
"定传输请求内容格式以及编码格式
http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
* 定调用服务
http_client->request->set_method( if_http_request=>co_request_method_get ).
"发送
CALL METHOD http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
"接收
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
"回去返回的状态
DATA:http_status_code TYPE i,
status_text TYPE string.
CALL METHOD http_client->response->get_status
IMPORTING
code = http_status_code
reason = status_text
.
"READ RESPONSE DATA 返回结果报文
DATA:w_result TYPE string.
CALL METHOD http_client->response->get_cdata
RECEIVING data = w_result.
"通过/UI2/CL_JSON=>generate() 来通过JSON来转化出内表
lr_data = /ui2/cl_json=>generate( json = w_result ).
"进行图1的转换 才能实现如下ASSIGN COMPONENT的操作 并且双击结果也不一样如图2,图3
ASSIGN lr_data->* TO <data>.
IF sy-subrc = 0.
ASSIGN COMPONENT 'SUCCESS' OF STRUCTURE <data> TO FIELD-SYMBOL(<success_ref>).
ASSIGN <success_ref>->* TO FIELD-SYMBOL(<success>).
ASSIGN COMPONENT 'RESULT' OF STRUCTURE <data> TO FIELD-SYMBOL(<list>).
IF sy-subrc EQ 0.
* ASSIGN <list>->* TO FIELD-SYMBOL(<l_strut>).
ASSIGN <list>->* TO <l_strut>.
lr_struct ?= cl_abap_typedescr=>describe_by_data( <l_strut> ). "结构转化
lt_comps_wa = lr_struct->components.
LOOP AT lt_comps_wa INTO DATA(ls_comps_wa). "循环获取对应数据
ASSIGN COMPONENT ls_comps_wa-name OF STRUCTURE <l_strut> TO FIELD-SYMBOL(<value_ref>).
ASSIGN <value_ref>->* TO FIELD-SYMBOL(<value>).
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_structure
*&---------------------------------------------------------------------*
FORM create_structure .
LOOP AT lt_comps_wa INTO DATA(wa_comps).
CLEAR wa_structure.
wa_structure-fieldname = wa_comps-name. "列名
wa_structure-inttype = 'C'. "数据类型
wa_structure-intlen = '30'. "长度
wa_structure-reptext = wa_comps-name. "标题文本
APPEND wa_structure TO it_structure.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_dynamic_table
*&---------------------------------------------------------------------*
FORM create_dynamic_table .
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_structure
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>. " 用表类型指针 <dyn_table> 指向 数据对象的内容.
ENDFORM. " create_dynamic_table
*&---------------------------------------------------------------------*
*& Form write_data_to_dyntable
*&---------------------------------------------------------------------*
FORM write_data_to_dyntable .
CREATE DATA dy_line LIKE LINE OF <dyn_table>. " 建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构
ASSIGN dy_line->* TO <dyn_wa>. " 用<dyn_wa>指针指向该结构
**动态字段赋值
DEFINE m_add_value.
ASSIGN COMPONENT &1 OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = &2.
UNASSIGN <dyn_field>.
END-OF-DEFINITION.
LOOP AT lt_comps_wa INTO DATA(ls_comps_wa).
ASSIGN COMPONENT ls_comps_wa-name OF STRUCTURE <l_strut> TO FIELD-SYMBOL(<value_ref>).
ASSIGN <value_ref>->* TO FIELD-SYMBOL(<value>).
m_add_value ls_comps_wa-name <value>.
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDFORM. " write_data_to_dyntable
*&---------------------------------------------------------------------*
*& Form show_alv_data
*&---------------------------------------------------------------------*
FORM show_alv_data.
LOOP AT it_structure INTO wa_structure..
ls_fieldcat-fieldname = wa_structure-fieldname.
ls_fieldcat-seltext_m = wa_structure-reptext.
APPEND ls_fieldcat to gt_fieldcat.
CLEAR ls_fieldcat.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = gt_fieldcat
TABLES
t_outtab = <dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
图1
图2
图3