I'm trying to upload a XML file from an android device to a URL where it will be parsed. As an example I was given a curl command, which works:
curl -F xml=@templ2.xml "http://the-url-to-the-server"
But when I try to send something from my Android device I keep getting 'Invalid xml' as response. So based on this answer I created a php file for myself to try sending the file to, to see how it gets send:
<?php
print_r($_FILES);
print_r($_REQUEST);
?>
I've been trying all kinds of example code, but they all are rather similar, so I think they must be the right way to do it.
Basically it comes down to this: Either I send the file without declaring the MIME-type (see foo below), and then it get send as "application/octet-stream"
, or I add a MIME-type "text/xml"
or "application/xml"
(see bar below) and then it ends up in the $_REQUEST
of php, which I assume means it was not send as a file.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(postReceiverUrl);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("foo", new FileBody(file));
reqEntity.addPart("bar", new FileBody(file, "text/xml"));
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);
gives
Array
(
[foo] => Array
(
[name] => templ2.xml
[type] => application/octet-stream
[tmp_name] => /tmp/phppWbgl8
[error] => 0
[size] => 203
)
)
Array
(
[bar] => <foo>
<bar>
</bar>
</foo>
)
Some related questions, which I have tried the code from: