2

由于我是 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();
      }
 }
4

2 回答 2

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);

方法 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();
}

参考

于 2013-10-25T11:16:01.260 回答
1

遵循本教程 1教程 2和关于发送 Xml 数据的答案。

对于任何查询,请让您的服务器人员调试您的请求并告诉您确切的问题。

于 2013-10-25T10:54:41.443 回答