gpt4 book ai didi

android - 如何在android中使用 Volley 发送(POST)xml文件

转载 作者:行者123 更新时间:2023-11-29 15:58:05 25 4
gpt4 key购买 nike

有没有办法在 Volley 中使用 POST 查询发送 xml 文件?这文件包含如下产品:

<?xml version="1.0" encoding="utf-8" ?>
<products_list>
<product>
<tag1></tag1>
<tag2></tag2>
<tag3></tag3>
</product>
<product>
<tag1></tag1>
<tag2></tag2>
<tag3></tag3>
</product>
...
</products_list>

我知道 Volley 有 StringRequest 但我如何将上面列出的文件传输到服务器?

最佳答案

Request

getBodyContentType()getBody()对象由 Volley 的 HurlStack 使用和 HttpClientStack获取要推送的数据。您应该覆盖这些方法并将请求指定为 POST。

改编自basic Volley training中提供的代码

// Create the request queue
RequestQueue queue = Volley.newRequestQueue(this);

// Create the request object
String url = "http://www.example.com/";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener() {

@Override
public void onResponse(String response) {
// TODO handle the response
}

},
new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO handle the error
}

}
) {

@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=" +
getParamsEncoding();
}

@Override
public byte[] getBody() throws AuthFailureError {
String postData = FooBar.getPostData(); // TODO get your final output
try {
return postData == null ? null :
postData.getBytes(getParamsEncoding());
} catch (UnsupportedEncodingException uee) {
// TODO consider if some other action should be taken
return null;
}
}

};

// Schedule the request on the queue
queue.add(stringRequest);

关于android - 如何在android中使用 Volley 发送(POST)xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26690921/

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