使用Android将XML发送到PHP服务器
我尝试了许多不同的解决方案,将XML文件发布到PHP服务器 响应总是“09-27 10:49:10.550:I/TAG(3950):无法识别文件MIME-TYPE。” 代码的最新版本为:使用Android将XML发送到PHP服务器,php,android,xml,http-post,multipart,Php,Android,Xml,Http Post,Multipart,我尝试了许多不同的解决方案,将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(fileN
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);
在出现如下连接的init之前:
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);
ContentBody fb = new FileBody(file, "text/xml");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", fb);
httppost.setEntity(entity);
return entity;
我也尝试过这个解决方案(以及许多不同的解决方案),但没有成功:
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);
有什么想法吗?我用这种方法解决了这个问题:
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);
ContentBody fb = new FileBody(file, "text/xml");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", fb);
httppost.setEntity(entity);
return entity;
我不知道这个问题是严格的还是标题设置。。不管怎样,现在它工作了……)