You are on page 1of 2

*&---------------------------------------------------------------------*

*& Report ZANUBHAV_CALL_EXT_SERVICE


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZANUBHAV_CALL_EXT_SERVICE.

TYPES: BEGIN OF ty_data,


ID type string,
Name type string,
Description type string,
ReleaseDate type string,
DiscontinuedDate type string,
Rating type i,
Price type i,
end of ty_data.
data: lt_data type TABLE OF ty_data.
"Step 1: Create an HTTP Client Object

cl_http_client=>create_by_destination(
EXPORTING
destination = 'ANUBHAVTRAINING' " Logical
destination (specified in function call)
IMPORTING
client = data(lo_http) " HTTP Client
Abstraction
EXCEPTIONS
argument_not_found = 1 " Connection Parameter
(Destination) Not Available
destination_not_found = 2 " Destination not found
destination_no_authority = 3 " No Authorization to Use HTTP
Destination
plugin_not_active = 4 " HTTP/HTTPS communication not
available
internal_error = 5 " Internal Error (e.g. name too
long)
others = 6
).
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

"Step 2: Make a Request

lo_http->send(
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
http_invalid_timeout = 4 " Invalid Time Entry
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.

"Step 3: Ask for response


lo_http->receive(
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing method
others = 4
).
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

"Step 4: Get the data in your collection

data(result) = lo_http->response->get_cdata( ).

"Step 5: Display Data

cl_abap_browser=>show_html(
EXPORTING
title = 'anubhavtrainings.com' " Window Title
html_string = result " HTML String
).

"Step 6: Parse this data JSON-->Itab or XML-->Itab


data: lv_final type string.

split result at '[' into table data(lt_stringtab).


READ TABLE lt_stringtab into data(ls_stringtab) INDEX 2.
CONCATENATE '[' ls_stringtab INTO lv_final.

"Built in class provided by SAP UI framework


/ui2/cl_json=>deserialize(
EXPORTING
json = lv_final " JSON string
pretty_name = /ui2/cl_json=>pretty_mode-camel_case "
Pretty Print property names
CHANGING
data = lt_data " Data to serialize
).

"Display data to the user - brought to you by AnubhavTrainings.com


cl_demo_output=>display_data(
EXPORTING
value = lt_data
* name = " Name
).

You might also like