Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to send xml in utf-8 over socket in Android
    text
    copied!<p>I have an Android app that sends xml files to a server. If I set the encoding of the xml header to utf-8 it all works until there is a weird character like a japanese symbol for example, in this case the server rejects the xml as not being utf-8. I have no control over how the server works but it is an established product so it isn't anything at their end.</p> <p>Right before sending the xml I print the string that contains it to terminal and all the characters show up correctly.</p> <p>How can I know if the string I've created really is utf-8 and if it is, how can I send it as utf-8 through a socket.</p> <p>This is how I read the file into a String:</p> <pre><code>file = new File (filePath); FileReader fr = new FileReader (file); BufferedReader br = new BufferedReader(fr); // Reading the file String line; while((line=br.readLine())!=null) data += line + '\n'; br.close(); </code></pre> <p>And this is how I send the String</p> <pre><code>Socket socketCliente = new Socket(); try { socketCliente.connect(new InetSocketAddress(address, port), 2000); } catch (UnknownHostException e) { getError("Host doesn't exist"); return -1; } catch (IOException e) { getError("Could not connect: The host is down"); return -1; } DataOutputStream serverOutput = null; try { serverOutput = new DataOutputStream(socketCliente.getOutputStream()); } catch (IOException e1) { getError("Could not get Data output stream"); } try { serverOutput.writeBytes(data); } catch (IOException e) { getError("Could not write on server"); } </code></pre> <p>If I print the "data" String right before sending it, all the characters show up correctly.</p> <p>I have tried countless different ways of reading into the string and writing to the socket in different ways but they either make no difference or stop the xml being accepted altogether even with standard characters.</p> <p><strong>SOLVED: Rather than reading from the file to a String I read to a byte array and then send that.</strong></p> <pre><code>file = new File(filePath); int size = (int) file.length(); data = new byte[size]; try { BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); buf.read(data, 0, data.length); buf.close(); } catch (FileNotFoundException e) { getError("File not found"); } catch (IOException e) { getError("Could not read from file"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload