
Android POST data a kódování
Chvilku jsem váhal, jestli tento dotaz vůbec pokládat, jestli bude zodpovězen, ale řekl jsem si nakonec jo.
Posílám z aplikace v Javě v Androidu data na PHP server:
Jde v podstatě o ekvivalent políčka ve formuláři v HTML.
Zde se políčko jmenuje NAME a má hodnotu : Kulaťoučký pes pěl strašné ódy
int serverResponseCode = 0;
HttpURLConnection connection;
DataOutputStream dataOutputStream;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead,bytesAvailable,bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File selectedFile = new File(selectedFilePath);
String[] parts = selectedFilePath.split("/");
final String fileName = parts[parts.length-1];
FileInputStream fileInputStream = new FileInputStream(selectedFile);
URL url = new URL("http://mujServerPHP/uploadFile.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);//Allow Inputs
connection.setDoOutput(true);//Allow Outputs
connection.setUseCaches(false);//Don't use a cached Copy
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("uploaded_file",selectedFilePath);
//creating new dataoutputstream
dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + "Name" + "\""+ lineEnd);
dataOutputStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes("\"Kulaťoučký pes pěl strašné ódy\"" + lineEnd);
dataOutputStream.flush();
Příklad funguje OK, jen mrší diakritiku, pokud se vyskytuje.
Jak prosím zjistím v jakém kódování to je a jak to vlastně opravím?
Na serveru to zapisuji takto do souboru:
file_put_contents ("uploads/peklo.txt", utf8_encode($_POST['Name']));