20170313 ABAP以jason 格式返回值到http(接口内容返回)

Posted Rainystuday

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20170313 ABAP以jason 格式返回值到http(接口内容返回)相关的知识,希望对你有一定的参考价值。

 问题1:

返回jason 格式信息给你们这步不通,

这个可以怎么处理,

 

***得到SCP 系统开发回复,他们需要调整方法;

 

 (1)调用函数做RETURN,

  IT_ZSMLSCPNOTICE-FUNCTIONNAME = \'ZMLSCP1_FR0005\'.
    IT_ZSMLSCPNOTICE-BCPCODE = BCPCODE.
    IT_ZSMLSCPNOTICE-STATUS = O_STATU.
    IT_ZSMLSCPNOTICE-MSG = O_MSG.
    IT_ZSMLSCPNOTICE-RESULT = \'\'.
    APPEND IT_ZSMLSCPNOTICE.
    CLEAR:IT_ZSMLSCPNOTICE.

    CALL FUNCTION \'ZMLSCP1_FR0018\'
      EXPORTING
        L_FNAME  = \'ZMLSCP1_FR0018\'
      IMPORTING
        EV_MSGTY = L_MSGTY
        EV_MSGTX = L_MSGTX
      TABLES
        IT_INPUT = IT_ZSMLSCPNOTICE.
*    O_STATU = L_MSGTY.
    O_MSG = \'调用返回结果函数: \' &&  L_MSGTX.
FUNCTION ZMLSCP1_FR0018 .
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(L_FNAME) TYPE  RS38L_FNAM OPTIONAL
*"  EXPORTING
*"     VALUE(EV_MSGTY) TYPE  CHAR1
*"     VALUE(EV_MSGTX) TYPE  STRING
*"  TABLES
*"      IT_INPUT STRUCTURE  ZSMLSCPNOTICE OPTIONAL
*"----------------------------------------------------------------------
  DATA: JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
        JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.

  DATA: LV_URL TYPE STRING."http 服务接口地址
  DATA: LO_HTTP_CLIENT  TYPE REF TO IF_HTTP_CLIENT.

  DATA: LV_LEN TYPE I."发送报文长度
  DATA: LV_RESPON TYPE STRING.
  DATA: LV_SUBRC TYPE SY-SUBRC.
  DATA: LV_JSON_STR TYPE STRING. "发送报文

  IF L_FNAME EQ SPACE.
    L_FNAME = \'ZMLSCP1_FR0018\'.
  ENDIF.

* 设置HTTP接口地址
  SELECT SINGLE ZADDR INTO LV_URL
    FROM ZMLSCP_URL_CFG
    WHERE SYSID EQ SY-SYSID AND FUNCTIONNAME EQ L_FNAME.
  IF LV_URL IS INITIAL.
    EV_MSGTY = \'E\'.
    EV_MSGTX = SY-SYSID && \'表ZMLSCP_URL_CFG未配置服务地址\'.
    EXIT.
  ENDIF.

* 内表->JSON 设置报文内容(json格式字符串)
  CREATE OBJECT JSON_SER
    EXPORTING
      DATA = IT_INPUT[].
  CALL METHOD JSON_SER->SERIALIZE.
  CALL METHOD JSON_SER->GET_DATA
    RECEIVING
      RVAL = LV_JSON_STR.

  "创建客户端请求
  CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
    EXPORTING
      URL                = LV_URL
    IMPORTING
      CLIENT             = LO_HTTP_CLIENT
    EXCEPTIONS
      ARGUMENT_NOT_FOUND = 1
      PLUGIN_NOT_ACTIVE  = 2
      INTERNAL_ERROR     = 3
      OTHERS             = 4.
  IF SY-SUBRC <> 0.
    CALL FUNCTION \'MESSAGE_TEXT_BUILD\'
      EXPORTING
        MSGID               = SY-MSGID
        MSGNR               = SY-MSGNO
        MSGV1               = SY-MSGV1
        MSGV2               = SY-MSGV2
        MSGV3               = SY-MSGV3
        MSGV4               = SY-MSGV4
      IMPORTING
        MESSAGE_TEXT_OUTPUT = EV_MSGTX.
    EV_MSGTY = \'E\'.
    EXIT.
  ENDIF.

  "设定传输请求内容格式以及编码格式
  LO_HTTP_CLIENT->REQUEST->SET_CONTENT_TYPE( CONTENT_TYPE = \'application/json; charset=utf-8\' ).

  "设定调用服务
  LO_HTTP_CLIENT->REQUEST->SET_METHOD( IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST ).

  "设置待传输内容
  LV_LEN = STRLEN( LV_JSON_STR ).
  CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
    EXPORTING
      DATA   = LV_JSON_STR
      OFFSET = 0
      LENGTH = LV_LEN.

  "发送请求
  LO_HTTP_CLIENT->SEND(  EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1
                                    HTTP_INVALID_STATE         = 2 ).

  IF SY-SUBRC <> 0.
    "操作失败,获取失败原因
    LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = EV_MSGTX ).
    EV_MSGTY = \'E\'.
    EXIT.
  ENDIF.

  "读取远程服务返回的处理过结果。
  LO_HTTP_CLIENT->RECEIVE( EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1
                                      HTTP_INVALID_STATE         = 2
                                      HTTP_PROCESSING_FAILED     = 3 ).

  IF SY-SUBRC <> 0 .
    LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = EV_MSGTX ).
    EV_MSGTY = \'E\'.
    EXIT.
  ELSE.
    "读取返回返回内容
    CLEAR LV_RESPON.
    LV_RESPON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
    MOVE LV_RESPON TO EV_MSGTX.
    EV_MSGTY = \'S\'.
  ENDIF.

ENDFUNCTION.
 11:37:47
返回JASON 内容
[{functionname: "ZMLSCP1_FR0005", bcpcode: "20170313001", status: "S", msg: "写入自建表成功!", result: ""}]
11:40:14
现在返回内容到这个地址http://deiap.changhong.com:18082/PM/ScpFunction/DispatchNotice 

 

20170313 测试问题:

  

LV_RESPON: {"isSuccessful":true,"Msg":"数据取得中..."}

SCP 那边反馈拿不到数据!!!

 

以上是关于20170313 ABAP以jason 格式返回值到http(接口内容返回)的主要内容,如果未能解决你的问题,请参考以下文章

CSDN日报20170313——《测定程序员的代码基因》

08使用后置处理器JASON Path Extractor将接口返回值传给另一个接口;

jason和XML的区别

HIVE json格式数据的处理

jMeter测试,jason Ectractor和正则表达式获取数组类型

ABAP 检查全角半角