0

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:

Community
  • 1
  • 1
Niels
  • 1,340
  • 2
  • 15
  • 32

2 Answers2

0

Instead of

reqEntity.addPart("bar", new FileBody(file, "text/xml"));

Try doing this

reqEntity.addPart("bar");
httpPost.addHeader("Content-Type", "application/xml");
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Then you would change the content type of the entire request, which I believe should be some form of multipart. Anyway I tried it and didn't help. – Niels Feb 11 '14 at 12:54
0

I figured out my problem. When I looked at the curl command, I realised xml=@templ2.xml means I had to use xml as name for the parameter. So

reqEntity.addPart("xml", new FileBody(file));
Niels
  • 1,340
  • 2
  • 15
  • 32