gpt4 book ai didi

java - Android - 使用 HttpURLConnection 将 XML 文件发送到 PHP API 服务器

转载 作者:行者123 更新时间:2023-12-01 11:12:00 24 4
gpt4 key购买 nike

目前,我正在尝试将一些数据以 XML 文件的形式导入到服务器。我已经成功登录并正在通过服务器的 API 执行所有操作。网站/服务器以 XML 形式响应,不确定这是否相关。

当我使用API​​的导入数据操作时,请求方法实际上是GET而不是POST,响应内容类型是text/xml。我想严格坚持使用 HttpURLConnection 并且我知道发送此 XML 文件将需要一些多部分内容类型的东西,但我不太确定如何从这里继续。我已经看过这两个示例,但它不适用于我的应用程序(至少不直接)。另外,我不太明白他们从哪里获得一些请求 header 。

Send .txt file, document file to the server in android

http://alt236.blogspot.ca/2012/03/java-multipart-upload-code-android.html

来自一位开发人员的消息说“要上传数据,请使用 action=importData&gwID=nnnn 并使用通常的方法多部分内容编码并将文件像往常一样放置在请求正文中。”

如何通过 API 将 XML 文件发送到服务器?

最佳答案

这就是你的做法:

    public void postToUrl(String payload, String address, String subAddress) throws Exception
{
try
{
URL url = new URL(address);
URLConnection uc = url.openConnection();
HttpURLConnection conn = (HttpURLConnection) uc;
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "text/xml");
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.write(payload);
pw.close();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
bis.close();

}
catch (Exception e)
{
e.printStackTrace();
}
}

关于java - Android - 使用 HttpURLConnection 将 XML 文件发送到 PHP API 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32236162/

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