FUNCTION zfunc_gettoken.
*"----------------------------------------------------------------------
*"*"本地接口:
*" EXPORTING
*" VALUE(TOKEN) TYPE STRING
*"----------------------------------------------------------------------
DATA:lv_abap TYPE REF TO cl_abap_conv_in_ce,
lc_url TYPE string,
ev_resp_code TYPE i,
ev_resp_reason TYPE string,
lo_http_client TYPE REF TO if_http_client,
lv_results TYPE string.
TYPES: BEGIN OF ty_header,
app_id TYPE string,
access_token TYPE string,
expires_in TYPE string,
END OF ty_header.
TYPES: BEGIN OF ty_token,
token TYPE ty_header,
END OF ty_token.
TYPES: BEGIN OF ty_body,
body TYPE ty_token,
END OF ty_body.
DATA:ls_data TYPE ty_body.
lc_url = 'URL'.
cl_http_client=>create_by_url(
EXPORTING
url = lc_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
IF sy-subrc <> 0.
RAISE create_by_url_failed.
ENDIF.
lo_http_client->request->set_method( if_http_request=>co_request_method_get ).
* Sending the request
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.
* Receiving the response
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc EQ 0.
lv_results = lo_http_client->response->get_cdata( ).
* lo_http_client->response->get_status(
* IMPORTING
* code = ev_resp_code
* reason = ev_resp_reason
* ).
ENDIF.
IF lv_results <> ''.
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_results
CHANGING
data = ls_data.
token = ls_data-body-token-access_token.
ENDIF.
ENDFUNCTION.
abap 多层jason 解析
最新推荐文章于 2024-04-25 21:51:26 发布