Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中将xml数据发布到服务器_Android_Xml - Fatal编程技术网

如何在android中将xml数据发布到服务器

如何在android中将xml数据发布到服务器,android,xml,Android,Xml,由于我是android新手,请为我提供教程链接,以便在服务器上发布xml数据。我面临的问题是执行post请求 public void uploadFileToServer() { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url_context + "/orders/order"); httppost.addHeader(

由于我是android新手,请为我提供教程链接,以便在服务器上发布xml数据。我面临的问题是执行post请求

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();
      }
 }
按照此操作,然后发送Xml数据


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

根据此示例创建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();
}

我已经发布了我的xmlString。请帮帮我。