今天是个娱乐的节日,学点新东西,如下:
突发奇想想要使用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名的国家柱状图:
可以看到我们查询的时候,米国的确诊人数已经到达了
就是做了一个简单的数据table 和 chart来学习一下