Android - Using HttpURLConnection to POST XML data

馋奶兔 提交于 2019-12-05 13:32:07

Yes, POST data goes as payload of your request. For example

URL url = new URL(theUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    String body = "<xml...</xml>";
    OutputStream output = new BufferedOutputStream(conn.getOutputStream());
    output.write(body.getBytes());
    output.flush();
finally {
    conn.disconnect();
}
dominicoder

I'm not sure if I'm understanding NamedValuePair right. Would it be better to create a string for my XML data and POST this way instead?

Your post seems to be cut off, but from what you show what you're doing is not posting XML but adding query parameters.

Convert your XML to an encoded string, then write it to the output stream you get from conn.getOutputStream().

Here's a similar example: https://stackoverflow.com/a/2737455/1197251

You would replace "query" with your XML string.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!