带有Volley的HTTP帖子-XML帖子,JSON响应 [英] HTTP Post with Volley--- XML post, JSON Response
问题描述
我正在尝试进行API调用,并且该API需要XML
格式:
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>
但是它向我发送了JSON
中的输出.我一直在尝试使用Volley
来实现这一点:
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;
}
我不确定使用Volley
是否存在问题,或者库是否不支持我完全尝试的操作.我认为我可能需要实现一个自定义请求类型,该类型将发送一个XML
字符串并返回一个JSON
对象,但是相对缺乏文档使我不确定如何执行此操作.任何帮助,我们将不胜感激,不胜感激!
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!
推荐答案
凌空抽射中有一种方法可以发送自定义主体.这是通过重写getBody()来完成的.您不需要重写getParams().而是将您的getParams()函数替换为以下代码段.
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();
}
这篇关于带有Volley的HTTP帖子-XML帖子,JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!