带有Volley的HTTP Post --- XML post,JSON Response

时间:2016-06-10 16:41:46

标签: android json xml post android-volley

我正在尝试进行API调用,API期望格式为XML

 <root>
<subject>Security</subject>
<request>GetSessionInfo</request>
<sessionGUID>999999999999</sessionGUID>
<userGUID></userGUID>
<emptyString/>
<lastUpdateDate>
<forceLoad>0</forceLoad>
<transporterFormat>2</transporterFormat>
<parms>            {
                            "sessionGUID":"99999999999999"
            }
            </parms>
</root>

但它在JSON向我发送了输出。我一直试图使用Volley完成此操作:

        RequestQueue mRequestQueue = RequestQueueSingleton
           .getInstance(this.getApplicationContext())
           .getRequestQueue();

        String targetURL = "http://api.myurl.com";

        StringRequest postRequest = new StringRequest(Request.Method.POST, targetURL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        userGUID = response;


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> parms = new HashMap< >();
                JSONObject jsonRequest = new JSONObject();
                try {
                    jsonRequest.put("sessionGUID", sessionGUID);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                parms.put("requestData",
                        getXML(SECURITY, GET_SESSION_INFO, "", sessionGUID, "", null, jsonRequest));
                return parms;
            }
        };
        postRequest.setTag("POST");
        System.out.println("we've attempted to use Volley");
        mRequestQueue.add(postRequest);


With the reference GetXML methods as follows: 

     public String getXML(String subject, String request, String userGUID, String sessionGUID,, Date lastUpdateDate, JSONObject parms) {
        StringBuilder stringBuilder = new StringBuilder();
        String date;
        if (lastUpdateDate == null) date = "";
        else date = lastUpdateDate.toString();
        stringBuilder.append("<root>\n <subject>" + subject + "</subject>\n")
        .append("<request>" + request + "</request>\n ")
                .append("<sessionGUID>" + sessionGUID + "</sessionGUID>\n")
                .append("<userGUID>" + userGUID + "</userGUID>\n")
                .append("<emptyString>" + "" + "</emptyString>\n ")
                .append("<lastUpdateDate>" + date + "</lastUpdateDate>\n ")
                .append("<forceLoad>0</forceLoad>\n" + " <transporterFormat>2</transporterFormat>\n")
                .append("<parms>" + parms.toString() + "</parms>\n" + "</root>");

        String result = stringBuilder.toString();
        System.out.println(result);
        return result;
    }

我不确定使用Volley是否存在问题,或者图书馆是否支持我完全尝试的操作。我认为我可能需要实现一个自定义请求类型,它发送一个XML字符串并返回一个JSON对象,但相对缺乏文档使我不确定如何做到这一点。任何帮助将不胜感激,抱歉长度!

1 个答案:

答案 0 :(得分:2)

我们可以通过排球方式发送自定义正文。这是通过重写getBody()来完成的。您不需要覆盖getParams()。而是使用下面的代码片段替换你的getParams()函数。

 @Override
    public byte[] getBody() throws AuthFailureError {
               JSONObject jsonRequest = new JSONObject();
                try {
                    jsonRequest.put("sessionGUID", sessionGUID);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
        String body = getXML(SECURITY, GET_SESSION_INFO, "", sessionGUID, "",  null, jsonRequest));
        return body.getBytes();
    }