Ethan Ethan - 6 months ago 28
JSON Question

HTTP Post with Volley--- XML post, JSON Response

I'm trying to make an API call, and the API expects

XML
of the format:

<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>


But it's sending me the output in
JSON
. I've been trying to accomplish this using
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;
}


I'm not sure if there's an issue with my use of
Volley
or if the library doesn't support the action that I'm attempting entirely. I think that I may need to implement a custom request type that sends an
XML
string and returns a
JSON
object, but the relative lack of documentation makes me unsure how to do that. Any help would be appreciated, sorry for the length!

Answer

There is a way in volley by which we can send custom body. This is done by overriding getBody(). You don't need to override getParams(). Instead replace your getParams() function with below code snippet.

 @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();
    }