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 に ">" がありません。一日中コピペしてたので言及してませんでした。ふふ…