简体   繁体   English

Android-将XML模式发布到PHP服务器API

[英]Android - Post XML schema to PHP server API

I'm trying to parse out an XML schema to my server. 我正在尝试将XML模式解析到我的服务器。

URL obj = new URL(url);
URLConnection uc = obj.openConnection();
HttpURLConnection conn = (HttpURLConnection) uc;
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("Content-Length", Integer.toString(filecontent.length())); 
conn.setRequestProperty("Content-Encoding", "gzip");

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(filecontent);
writer.close();

where filecontent is my XML schema in a String format. 其中filecontent是我的XML模式(字符串格式)。 If I try to read the response of my server with 如果我尝试读取服务器的响应

BufferedReader in = new BufferedReader(new 
InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null ) {
     response.append(inputLine);
    }
in.close();

my app will hang and EVENTUALLY, it'll be able to finish but the response will be empty. 我的应用程序将挂起,并且可以正常完成,但响应为空。 Not parsing/writing any content will allow me to read the response of the server and it won't hang. 不解析/编写任何内容将允许我读取服务器的响应,并且不会挂起。 The view is in XML which gives me a XML Parse Error with an EXCEPTION in CDATA: exception 'Exception' with message 'String could not be parsed as XML'. 该视图以XML格式提供,这给了我XML解析错误,并在CDATA中出现EXCEPTION:消息“字符串无法解析为XML”的异常“ Exception”。

I have a C# version of the code that works for sure and comparing my Java code with the C# code, it looks quite similar except for one thing: C# writes out an XmlDocument for the content with XmlWriter for the writer which I don't believe Java is capable of doing. 我有一个肯定能正常工作的C#版本代码,并将我的Java代码与C#代码进行了比较,除了一件事情外,它看起来非常相似:C#用XmlWriter编写了一个XmlDocument内容供作者使用,我不相信Java有能力做到。 I can paste the C# code here if needed. 如果需要,我可以在此处粘贴C#代码。

Anyone know whats wrong? 有人知道怎么了吗? Its either the format of the XML schema is wrong so the server isn't accepting it or that it doesn't accept strings? 它是XML模式的格式错误,还是服务器不接受它,或者它不接受字符串? Or perhaps it isn't equivalent to the C# code? 还是不等同于C#代码? I'm at a loss as to the reason why its not working, any help would be appreciated. 我不知道为什么它不起作用,任何帮助将不胜感激。

"where filecontent is my XML schema in a String format" - implies your data is uncompressed text, but you tell the server it is encoded with gzip on this line -- “其中filecontent是我的XML模式(采用String格式)”-表示您的数据是未压缩的文本,但是您告诉服务器这行使用gzip编码-

conn.setRequestProperty("Content-Encoding", "gzip");

The server is likely trying to un-zip your string and ending up in a confused state. 服务器可能会尝试解压缩您的字符串并最终陷入混乱状态。 Try removing that line. 尝试删除该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM