gpt4 book ai didi

android - 带 Volley 的 HTTP 发布 --- XML 发布,JSON 响应

转载 作者:数据小太阳 更新时间:2023-10-29 02:34:02 32 4
gpt4 key购买 nike

我正在尝试进行 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 对象,但是相对缺乏文档让我不确定如何去做.任何帮助将不胜感激,抱歉太长了!

最佳答案

在 volley 中有一种方法可以发送自定义正文。这是通过覆盖 getBody() 来完成的。您不需要覆盖 getParams()。而是用以下代码 fragment 替换您的 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();
}

关于android - 带 Volley 的 HTTP 发布 --- XML 发布,JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37753288/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com