簡體   English   中英

Java HttpResponse 提取數據

[英]Java HttpResponse Extract data

我有一個java代碼。 此代碼執行 http 請求並獲得響應。 返回多個對象的響應。 我想創建一個 Map<String,String> 的 ArrayList

每個地圖實例將存儲一個對象。 如何處理響應並存儲在地圖的數組列表中

這是我的代碼

 package com.mycompany.sfmc_test_kpmgnl; import com.exacttarget.fuelsdk.ETSdkException; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Properties; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import org.apache.http.util.EntityUtils; public class KPMG_Get_BusinessUnits { public static void main(String[] args) throws ETSdkException, FileNotFoundException, IOException, UnirestException, SOAPException { FileReader reader = new FileReader("D:\\\\Works\\\\Java\\\\SFMC_Test_KPMGNL\\\\src\\\\main\\\\java\\\\com\\\\mycompany\\\\sfmc_test_kpmgnl\\\\fuelsdk.properties"); Properties prop = new Properties(); prop.load(reader); String TenentId = prop.getProperty("TenantId"); SFMC_GetAccessToken Tokenclass = new SFMC_GetAccessToken(); HashMap<String, Object> Token = Tokenclass.TokenInfo(); String TokenValue = (String) Token.get("access_token"); Integer Token_Expiry = (Integer) Token.get("expires_in"); String token_type_value = (String) Token.get("token_type"); String payload = get_BU_Payload(TokenValue,TenentId); Unirest.setTimeouts(0, 0); HttpResponse<String> response = Unirest.post("https://"+TenentId+".soap.marketingcloudapis.com/Service.asmx") .header("Content-Type", "text/xml") .header("Authorization", "Bearer"+TokenValue) .header("Action", "Retrieve") .body(payload).asString(); if(response.getStatus()==200){ //ArrayList<Map<String,String>> BU_Data = new ArrayList<Map<String,String>>(); System.out.println(response.getBody()); } } static String get_BU_Payload(String AccessToken,String TenentId) throws SOAPException, IOException { String BU_Payload = ""; MessageFactory factory = MessageFactory.newInstance(); SOAPMessage soapMsg = factory.createMessage(); SOAPPart part = soapMsg.getSOAPPart(); //Envelope SOAPEnvelope envelope = part.getEnvelope(); envelope.setPrefix("s"); envelope.addNamespaceDeclaration("a", "http://schemas.xmlsoap.org/ws/2004/08/addressing"); envelope.addNamespaceDeclaration("u", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); //Header SOAPHeader header = envelope.getHeader(); header.setPrefix("s"); header.addChildElement("Action","a").addTextNode("Retrieve").setAttribute("s:mustUnderstand", "1"); header.addChildElement("To","a").addTextNode("https://"+TenentId+".soap.marketingcloudapis.com/Service.asmx").setAttribute("s:mustUnderstand", "1"); header.addChildElement("fueloauth","a").addTextNode(AccessToken).setAttribute("xmlns", "http://exacttarget.com"); //Body SOAPBody body = envelope.getBody(); body.setPrefix("s"); body.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); body.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); SOAPBodyElement Wrapper_Method_Element = (SOAPBodyElement) body.addChildElement("RetrieveRequestMsg","","http://exacttarget.com/wsdl/partnerAPI"); SOAPBodyElement Method_Element = (SOAPBodyElement) Wrapper_Method_Element.addChildElement("RetrieveRequest"); Method_Element.addChildElement("ObjectType").addTextNode("BusinessUnit"); Method_Element.addChildElement("QueryAllAccounts").addTextNode("true"); Method_Element.addChildElement("Properties").addTextNode("Name"); Method_Element.addChildElement("Properties").addTextNode("AccountType"); Method_Element.addChildElement("Properties").addTextNode("BusinessName"); Method_Element.addChildElement("Properties").addTextNode("ParentID"); Method_Element.addChildElement("Properties").addTextNode("CustomerKey"); Method_Element.addChildElement("Properties").addTextNode("EditionID"); Method_Element.addChildElement("Properties").addTextNode("FromName"); Method_Element.addChildElement("Properties").addTextNode("ID"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); soapMsg.writeTo(stream); BU_Payload = new String(stream.toByteArray(), "utf-8") ; return BU_Payload; } }

我得到的回應

 <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Header> <wsa:Action>RetrieveResponse</wsa:Action> <wsa:MessageID>urn:uuid:533e4809-2358-4302-b7f4-65d27107fc9b</wsa:MessageID> <wsa:RelatesTo>urn:uuid:956a1c31-85e6-4a31-8722-bdb215764ad6</wsa:RelatesTo> <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To> <wsse:Security> <wsu:Timestamp wsu:Id="Timestamp-745764aa-1012-40ed-853e-99576a471b4c"> <wsu:Created>2021-11-10T20:56:21Z</wsu:Created> <wsu:Expires>2021-11-10T21:01:21Z</wsu:Expires> </wsu:Timestamp> </wsse:Security> </soap:Header> <soap:Body> <RetrieveResponseMsg xmlns="http://exacttarget.com/wsdl/partnerAPI"> <OverallStatus>OK</OverallStatus> <RequestID>9b218589-3484-4ee5-9386-098449b30061</RequestID> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004255</ID> <ObjectID xsi:nil="true" /> <CustomerKey>ffsdfsfdsf</CustomerKey> <AccountType>ENTERPRISE_2</AccountType> <ParentID>0</ParentID> <Name>KPMG NV</Name> <FromName>KPMG NV</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>32</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004263</ID> <ObjectID xsi:nil="true" /> <CustomerKey>gfdhfghgf</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Shekhar</Name> <FromName>Shekhar Dautpure</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004264</ID> <ObjectID xsi:nil="true" /> <CustomerKey>vcbcvcvbvcb</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Jeroen</Name> <FromName>Jeroen Haster</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004265</ID> <ObjectID xsi:nil="true" /> <CustomerKey>hfghfghgf</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Kemal</Name> <FromName>Kemal Falatehan</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004267</ID> <ObjectID xsi:nil="true" /> <CustomerKey>dfhfghgfh</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Faysal</Name> <FromName>Faysal Mataich</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004269</ID> <ObjectID xsi:nil="true" /> <CustomerKey>fgfgjgjghjhg</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Training</Name> <FromName>Shekhar Dautpure</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510004891</ID> <ObjectID xsi:nil="true" /> <CustomerKey>dgfhfghfghgfhgf</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>SG_DEMO</Name> <FromName>KPMG Singapore</FromName> <BusinessName>KPMG Singapore</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510005831</ID> <ObjectID xsi:nil="true" /> <CustomerKey>fddffdghfghgf</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Ivor</Name> <FromName>Ivor</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510005853</ID> <ObjectID xsi:nil="true" /> <CustomerKey>cvbbnbghnm</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Jurrien Vermolen</Name> <FromName>Jurrien Vermolen</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510005854</ID> <ObjectID xsi:nil="true" /> <CustomerKey>fghfgfghfggfh</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>David van Daalen</Name> <FromName>David van Daalen</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510005855</ID> <ObjectID xsi:nil="true" /> <CustomerKey>fdgdgdfgdfgfdg</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Jeroen Slijkerman</Name> <FromName>Jeroen Slijkerman</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510005856</ID> <ObjectID xsi:nil="true" /> <CustomerKey>dfgdfgdfgdfgfdgfd</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Kay van der Vleuten</Name> <FromName>Kay van der Vleuten</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> <Results xsi:type="BusinessUnit"> <PartnerKey xsi:nil="true" /> <ID>510005857</ID> <ObjectID xsi:nil="true" /> <CustomerKey>fdfgfdgfdgfdg</CustomerKey> <AccountType>BUSINESS_UNIT</AccountType> <ParentID>510004255</ParentID> <Name>Kim van Kaam</Name> <FromName>Kim van Kaam</FromName> <BusinessName>KPMG NV</BusinessName> <EditionID>0</EditionID> <Subscription xsi:nil="true" /> </Results> </RetrieveResponseMsg> </soap:Body> </soap:Envelope>

我想將每個“結果”映射到一張地圖。 所以在這種情況下 ArrayList 將包含 13 個映射。 第一張地圖將是 Map1{ "ID":"510004255", "CustomerKey":"ffsdfsfdsf" ... }

我應該如何處理響應以創建這樣的地圖 arrarylist

我能夠使用文檔構建器和解析響應正文來解決問題。

 if(response.getStatus()==200){ ArrayList<Map<String,String>> BU_Data = new ArrayList<Map<String,String>>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document res = builder.parse(new InputSource(new StringReader(response.getBody()))); NodeList resultList = res.getElementsByTagName("Results"); for(int i=0;i<resultList.getLength();i++){ Node p = resultList.item(i); if(p.getNodeType()==Node.ELEMENT_NODE){ Element BU_Info = (Element) p; NodeList ResultList = BU_Info.getChildNodes(); for(int j=0;j<ResultList.getLength();j++){ Node n = ResultList.item(j); if(n.getNodeType()==Node.ELEMENT_NODE){ Element Result_Value = (Element) n; System.out.println(Result_Value.getTagName()+" "+Result_Value.getTextContent()); } } } System.out.println("------------------"); } }

一件重要的事情是builder.parse。 它需要一個輸入源和字符串閱讀器。 如果您不使用它,您可能會收到格式錯誤的 URL 異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM