【问题标题】:How to make XML api request with header, body and params?如何使用标头、正文和参数发出 XML api 请求?
【发布时间】:2016-09-03 10:40:57
【问题描述】:

我正在使用https://gist.github.com/itsalif/6149365 库进行 XML 请求。 它使用 Simple-XML 将 XML 序列化为对象。

当没有与 SOAP api 关联的标头和参数时,我能够成功发出 XML 请求。但是,在执行包含标头和参数的小复杂请求时,我无法获得响应。

我的请求格式如下所示

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.holidaywebservice.com/HolidayService_v2/">
 <SOAP-ENV:Body>
   <ns1:GetHolidaysAvailable>
     <ns1:countryCode>UnitedStates</ns1:countryCode>
   </ns1:GetHolidaysAvailable>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

代码

HashMap<String, String> mapData = new HashMap<>();
final String mRequestBody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.holidaywebservice.com/HolidayService_v2/\">\n" +
        " <SOAP-ENV:Body>\n" +
        "   <ns1:GetHolidaysAvailable>\n" +
        "     <ns1:countryCode>UnitedStates</ns1:countryCode>\n" +
        "   </ns1:GetHolidaysAvailable>\n" +
        " </SOAP-ENV:Body>\n" +
        "</SOAP-ENV:Envelope>";

SimpleXmlRequest dellaRequest = new SimpleXmlRequest<String>
        (Request.Method.POST, "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl", String.class, headerDataMap, bodyDataMap,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Log.d("BaseActivity", "response = " + response.toString());

                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Log.d("BaseActivity", "Error:" + error.getMessage());

                    }
                }
        ){
    @Override
    public byte[] getBody() throws AuthFailureError {

        try {
            return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, "utf-8");
            return null;
        }
    }
};

我收到 400 响应代码,即 BAD REQUEST,说明提供了无效输入,例如验证错误或缺少数据。

这个 api 在 Postman REST 客户端中运行良好。所以我无法弄清楚我做错了什么。

【问题讨论】:

    标签: android xml-parsing android-volley simple-framework


    【解决方案1】:

    您可以使用 Volley 有效地调用此 XML 请求。

    String url = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx";
    
    
    //Volley request
    StringRequest request = new StringRequest(Request.Method.POST, url,
    
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
    
                    Log.d("Response", response);
    
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                    Log.d("Error", error.getMessage());
    
                }
            }) {
    
        @Override
        public String getBodyContentType() {
            // set body content type
            return "text/xml; charset=UTF-8";
        }
    
        @Override
        public byte[] getBody() throws AuthFailureError {
    
            try {
                return reqXML.getBytes("UTF-8");
            } catch (UnsupportedEncodingException uee) {
                // TODO consider if some other action should be taken
                return null;
            }
        }
    
    };
    //
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    
    //Adding request to the queue
    requestQueue.add(request);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多