Question

I've tried a lot of different solutions to post an XML file to a PHP server.

The response is always "09-27 10:49:10.550: I/TAG(3950): File MIME-TYPE was not recognized."

The last version of the code is:

    httppost.setHeader("Content-Type","text/xml;charset=\"UTF-8\"");

    String textToUpload = "";
    try {
        textToUpload = getStringFromFile(fileName);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new StringEntity(textToUpload);

Before there is the init of the connection like this:

private void openConnection () throws IOException {
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost(url);
}

And after the response is handled in this way:

private int getServerResponse() throws IOException {
    HttpResponse response = httpclient.execute(httppost);
    Log.i("CIAO", EntityUtils.toString(response.getEntity()));
    StatusLine statusLine = response.getStatusLine();
    return statusLine.getStatusCode();
}

I have tried also this solution (and a lot of different ones) but without any success:

    File file = new File(fileName);
    httppost.setHeader("Content-Type","text/xml;charset=UTF-8");

    ContentBody fb = new FileBody(file, "text/xml");
    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.STRICT);
    entity.addPart("file", fb);
    httppost.setEntity(entity);

Any idea?

Was it helpful?

Solution

I've solved the problem in this way:

    File file = new File(fileName);

    ContentBody fb = new FileBody(file, "text/xml");
    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("file", fb);
    httppost.setEntity(entity);
    return entity;

I don't know whether the problem was STRICT or the header set.. anyway, now it works.. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top