0

I'm trying to parse out an XML schema to my server.

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. 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'.

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. I can paste the C# code here if needed.

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? Or perhaps it isn't equivalent to the C# code? I'm at a loss as to the reason why its not working, any help would be appreciated.

5
  • User the class DataOutputStream and call writer.writeBytes(filecontent); followed by writer.flush(); Commented Sep 5, 2015 at 0:06
  • App still hangs if I try to read the response, I'm assuming because it gets stuck in the whole loop for some reason. After a decently long time (2-3 minutes), the response is null. If I don't try to get the response, then it won't hang but the server does not reflect that my XML schema actually went through. I'll update my main post with some additional information Commented Sep 5, 2015 at 0:18
  • What are you using to generate the xml string? Have you checked what the output looks like? Commented Sep 5, 2015 at 0:31
  • I've been using XmlSerializer and yes, I do output everything into a file so I can see what it actually looks like. It seems to match an example that I have. Commented Sep 8, 2015 at 16:26
  • I added conn.setReadTimeout(10000); because realistically, it shouldn't take more than 10 seconds to do this. I get a timeout exception so would it be helpful to post the entire exception here? Commented Sep 8, 2015 at 19:56

1 Answer 1

1

"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 --

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That sounds likely, perhaps it was intended to be conn.setRequestProperty("Accept-Encoding", "gzip");
Thought this would be the answer as it made perfect sense! Unfortunately, it didn't solve my problem and the same thing still happens. Should I be copying all the request headers EXACTLY as when I type the API URL into my browser on my laptop? Headers like Content-Type are in the response headers and not the request headers. I tried removing it and changing it to conn.setRequestProperty("Accept-Encoding", "gzip");
So I ended up adding an <xml> tag to my xml string so it would now be writer.write("<xml>" + filecontent + "</xml>"); and that seemed to prevent it from hanging as well. I'm now getting a response from the API of the website of: <schema></schema> with nothing in between the schema tags. No idea what that means but my problem related to the app hanging is solved :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.