我尝试了很多不同的解决方案来将 XML 文件发布到 PHP 服务器。
响应始终为“09-27 10:49:10.550:I/TAG(3950):文件 MIME-TYPE 未被识别”。
代码的最后一个版本是:
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);
在这样的连接初始化之前:
private void openConnection () throws IOException {
httpclient = new DefaultHttpClient();
httppost = new HttpPost(url);
}
并且在以这种方式处理响应之后:
private int getServerResponse() throws IOException {
HttpResponse response = httpclient.execute(httppost);
Log.i("CIAO", EntityUtils.toString(response.getEntity()));
StatusLine statusLine = response.getStatusLine();
return statusLine.getStatusCode();
}
我也尝试过这个解决方案(以及许多不同的解决方案)但没有任何成功:
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);
任何想法?