因为我是android新手请提供本教程的链接,以便在服务器上发布xml数据。我面临的问题是执行帖子请求
public void uploadFileToServer()
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url_context + "/orders/order");
httppost.addHeader("Accept", "text/xml");
httppost.addHeader("Content-Type", "application/xml");
try
{
StringEntity entity = new StringEntity(xmlString, "UTF-8");
entity.setContentType("application/xml");
httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String strResponse = null;
if (response != null)
{
try {
strResponse = responseHandler.handleResponse(response);
} catch (HttpResponseException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
答案 0 :(得分:5)
根据此示例创建您的xml文件
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Order");
doc.appendChild(rootElement);
//set attribute to class
Attr Rattr = doc.createAttribute("Order_atrribute");
Rattr.setValue(curtrade);
rootElement.setAttributeNode(Rattr);
// companyid elements
Element staff = doc.createElement("companyid");
rootElement.appendChild(staff);
// shorten way
// staff.setAttribute("id", "1");
// firstname elements
Element firstname = doc.createElement("orderitems");
firstname.appendChild(doc.createTextNode("hii"));
staff.appendChild(firstname);
并将内容写入xml文件
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(this.getFilesDir().getAbsolutePath(),"file.xml"));
//String s=this.getFilesDir().getAbsolutePath();
// Output to console for testing
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
通过
发布您的文件DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url_context + "/orders/order.php");
String filePath = this.getFilesDir().getAbsolutePath();
File f=new File(filePath,"file.xml");
//byte[] data = FileOperator.readBytesFromFile(f);
String content=getFileContents(f);
StringEntity se = new StringEntity( content, HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setEntity(se);
f.delete();
HttpResponse httpresponse = httpClient.execute(httppost);
Log.d("xml1", httpresponse.toString());
HttpEntity resEntity = httpresponse.getEntity();
Log.d("xml2", resEntity.toString());
String result1 = EntityUtils.toString(resEntity);
Log.d("xml", "writer = "+result1);
methode getFileContent是
public String getFileContent(final File file) throws IOException {
final InputStream inputStream = new FileInputStream(file);
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
final StringBuilder stringBuilder = new StringBuilder();
boolean done = false;
while (!done) {
final String line = reader.readLine();
done = (line == null);
if (line != null) {
stringBuilder.append(line);
}
}
reader.close();
inputStream.close();
return stringBuilder.toString();
}
答案 1 :(得分:1)
在发送Xml数据上关注此tutorial 1,tutorial 2和此answer。
对于任何查询,请询问您的服务器人调试您的请求并告诉您确切的问题。