Java HTTP POST请求XML创建

Java HTTP POST请求XML创建,java,android,xml,http-post,Java,Android,Xml,Http Post,我想在Android活动中发出HTTP POST请求。我(认为我)知道如何这样做,但我的问题是我不知道如何创建XML文件。我尝试过以前文章中描述的不同方法,但我没有做到这一点 我的xml格式如下所示: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <IAM version="1.0"> <ServiceRequest> <RequestTimestamp&g

我想在Android活动中发出HTTP POST请求。我(认为我)知道如何这样做,但我的问题是我不知道如何创建XML文件。我尝试过以前文章中描述的不同方法,但我没有做到这一点

我的xml格式如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>    
<IAM version="1.0">
    <ServiceRequest>
        <RequestTimestamp>2012-07-20T11:10:12Z</RequestTimestamp
        <RequestorRef>username</RequestorRef>
        <StopMonitoringRequest version="1.0">
            <RequestTimestamp>2012-07-20T11:10:12Z</RequestTimestamp>
            <MessageIdentifier>12345</MessageIdentifier>
            <MonitoringRef>112345</MonitoringRef>
        </StopMonitoringRequest>
    </ServiceRequest>
</IAM>
编辑

尽管我设法使用以下几行创建了xml,但得到的结果并不正确

StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
sb.append("<IAM version'1.0'>");
sb.append("<ServiceRequest>");
sb.append("<RequestTimestamp>2012-07-20T12:33:00Z</RequestTimestamp");
sb.append("<RequestorRef>username</RequestorRef>");
sb.append("<StopMonitoringRequest version='1.0'>");
sb.append("<RequestTimestamp>2012-07-20T12:33:00Z</RequestTimestamp>");
sb.append("<MessageIdentifier>12345</MessageIdentifier>");
sb.append("<MonitoringRef>32900109</MonitoringRef>");
sb.append("</StopMonitoringRequest>");
sb.append("</ServiceRequest>");
sb.append("</IAM>");
String xmlContentTosend = sb.toString();

StringEntity entity = new StringEntity(xmlContentTosend, "UTF-8");
httpPost.setEntity(entity);
httpPost.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("username", "password"), "UTF-8", false));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
StringBuilder sb=新建StringBuilder();
某人加上(“”);
某人加上(“”);
某人加上(“”);

sb.在xml结构的第一个RequestTimestamp处追加(“2012-07-20T12:33:00Z”)。我已经复制粘贴了一整天,所以我没有提到它。Pfff…

您可以使用Dom解析器来完成

这里有一些代码

public class WriteXMLFile {

    public static void main(String argv[]) {

      try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("company");
        doc.appendChild(rootElement);

        // staff elements
        Element staff = doc.createElement("Staff");
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("id");
        attr.setValue("1");
        staff.setAttributeNode(attr);

        // shorten way
        // staff.setAttribute("id", "1");

        // firstname elements
        Element firstname = doc.createElement("firstname");
        firstname.appendChild(doc.createTextNode("yong"));
        staff.appendChild(firstname);

        // lastname elements
        Element lastname = doc.createElement("lastname");
        lastname.appendChild(doc.createTextNode("mook kim"));
        staff.appendChild(lastname);

        // nickname elements
        Element nickname = doc.createElement("nickname");
        nickname.appendChild(doc.createTextNode("mkyong"));
        staff.appendChild(nickname);

        // salary elements
        Element salary = doc.createElement("salary");
        salary.appendChild(doc.createTextNode("100000"));
        staff.appendChild(salary);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("C:\\file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
    }
}
这将创建一个类似xml的

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<company>
    <staff id="1">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
</company>

勇
木金
mkyong
100000

要通过http post发送,请执行以下操作:

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.192.131/");

    try {
        StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setEntity(se);

        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpresponse.getEntity();
        tvData.setText(EntityUtils.toString(resEntity));

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://192.168.192.131/");
试一试{

StringEntity se=new StringEntity(“而不是XML。它更高效、更易于使用。

您可以使用Dom解析器来实现这一点

这里有一些代码

public class WriteXMLFile {

    public static void main(String argv[]) {

      try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("company");
        doc.appendChild(rootElement);

        // staff elements
        Element staff = doc.createElement("Staff");
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("id");
        attr.setValue("1");
        staff.setAttributeNode(attr);

        // shorten way
        // staff.setAttribute("id", "1");

        // firstname elements
        Element firstname = doc.createElement("firstname");
        firstname.appendChild(doc.createTextNode("yong"));
        staff.appendChild(firstname);

        // lastname elements
        Element lastname = doc.createElement("lastname");
        lastname.appendChild(doc.createTextNode("mook kim"));
        staff.appendChild(lastname);

        // nickname elements
        Element nickname = doc.createElement("nickname");
        nickname.appendChild(doc.createTextNode("mkyong"));
        staff.appendChild(nickname);

        // salary elements
        Element salary = doc.createElement("salary");
        salary.appendChild(doc.createTextNode("100000"));
        staff.appendChild(salary);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("C:\\file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
    }
}
这将创建一个类似xml的

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<company>
    <staff id="1">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
</company>

勇
木金
mkyong
100000

要通过http post发送,请执行以下操作:

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.192.131/");

    try {
        StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setEntity(se);

        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpresponse.getEntity();
        tvData.setText(EntityUtils.toString(resEntity));

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://192.168.192.131/");
试一试{

StringEntity se=new StringEntity(“而不是XML。它更高效、更易于使用。

使用java希望以XML创建http POST请求,如下所示:

        <?xml version="1.0" encoding="UTF-8"?>
        <xmlActivityRequest version="2.0" xmlns="http://www.request.com/schema">
            <authentication>
                <user>pranab</user>
                <password>pranab</password>
            </authentication>
            <actionDate>2019-03-28</actionDate>
            <transactionOnly>false</transactionOnly>
        </xmlActivityRequest>
欲了解更多信息,请访问:

要使用java创建xml格式的http POST请求,如下所示:

        <?xml version="1.0" encoding="UTF-8"?>
        <xmlActivityRequest version="2.0" xmlns="http://www.request.com/schema">
            <authentication>
                <user>pranab</user>
                <password>pranab</password>
            </authentication>
            <actionDate>2019-03-28</actionDate>
            <transactionOnly>false</transactionOnly>
        </xmlActivityRequest>
欲了解更多信息,请访问:

我发送请求的服务器不是我的。据我所知,他们只接受xml。好的,我创建了文档。我如何从中创建要添加到httpPost的实体?谢谢你的回答。我已经尝试了所有方法。如果你检查我的问题,问题是我没有从服务器收到完整的答案。我不知道为什么会发生这种情况很好。当我在Firefox上使用HTTP资源测试时,一切都正常运行。当我自己尝试时,我收到一个部分答案。我发送请求的服务器不是我的。据我所知,他们只接受xml。好的,我创建了文档。我如何从中创建实体以添加到httpPost?谢谢你的回答。我已经尝试了所有方法g、 如果你检查我的问题,问题是我没有从服务器收到完整的答案。我不知道为什么会发生这种情况。当我在Firefox上使用HTTP资源测试时,一切正常。当我自己尝试时,我收到部分答案。你得到的响应是什么?你得到的响应是什么?