0

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>

次の Java コード行を記述しました。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

//What to write here to add the above XML lines?

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

編集

次の行を使用してなんとか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);

私が持つべき完全な答えではないという文字列ファイル (xml) が返されます。Firefox の HTTP Resource Test を使用すると正しい答えが得られますが、私のソリューションでは部分的な答えが得られます。を削除したときに、HTTP リソース テストで同じ部分的な回答を受け取ることができました。

<IAM version="1.0"> 

行またはその他の行 (一般に、xml の構造を「破棄」する場合)。ただし、関係があるかどうかはわかりません。

編集 (解決策が見つかりました) 見つけ られますか? XML 構造の最初の RequestTimestamp に ">" がありません。一日中コピペしてたので言及してませんでした。ふふ…

4

2 に答える 2

4

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>

ソース。

httpポスト経由で送信するには:

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

ところで、XML の代わりにJSONを使用することを検討してください。より効率的で使いやすくなっています。

于 2012-07-20T12:57:25.780 に答える