【问题标题】:Add DOCTYPE to retrofit POST xml request body添加 DOCTYPE 以改造 POST xml 请求正文
【发布时间】:2018-05-17 10:35:10
【问题描述】:

我正在使用改造 2 和 java 进行 API 调用。我已成功发出一些 POST 请求,但尝试添加新呼叫让我感到难过。

所需正文如下:

<!DOCTYPE ourapi SYSTEM "nameOfTheDTD.dtd">  
<mainPart version="7.0">  
   <some other stuff>
</mainPart>  

我可以使用常用的@Root、@Path 和@Element simpleframework.xml 注释构建请求正文的主要部分,但不知道如何添加 DOCTYPE 行。

谁能指点我正确的方向?

【问题讨论】:

    标签: java xml retrofit2


    【解决方案1】:

    我找不到使用注释来构建 xml 对象的任何好方法,因此解决方法是将 xml 创建为字符串,然后将其解析为 text/plain 以创建 okhttp3 RequestBody 并执行该操作。

    String xml = "<!DOCTYPE ourapi SYSTEM \"dtdName.dtd\">\n" +
                "<otherStuff>"\n+
                "</otherStuff>";
    
    
    RequestBody requestBody = 
             RequestBody.create(MediaType.parse("text/plain"), xml);
    
    return ourApi.post(requestBody).execute(); 
    

    【讨论】:

      猜你喜欢
      • 2019-03-23
      • 2016-11-20
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2020-02-25
      • 2015-09-24
      相关资源
      最近更新 更多