मैं नीचे फ़ंक्शन का उपयोग वेब सेवा को प्रतिक्रिया प्राप्त करने:प्रारूपण वेब सेवा प्रतिक्रिया
private String getSoapResponse (String url, String host, String encoding, String soapAction, String soapRequest) throws MalformedURLException, IOException, Exception {
URL wsUrl = new URL(url);
URLConnection connection = wsUrl.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[soapRequest.length()];
buffer = soapRequest.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Host", host);
if (encoding == null || encoding == "")
encoding = UTF8;
httpConn.setRequestProperty("Content-Type", "text/xml; charset=" + encoding);
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpConn.setRequestProperty("SOAPAction", soapAction);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();
InputStreamReader is = new InputStreamReader(httpConn.getInputStream());
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
sb.append(read);
read = br.readLine();
}
String response = decodeHtmlEntityCharacters(sb.toString());
return response = decodeHtmlEntityCharacters(response);
}
लेकिन इस कोड के साथ मेरी समस्या यह विशेष वर्ण के बहुत सारे देता है और XML अमान्य की संरचना बनाता है।
उदाहरण प्रतिक्रिया:
<PLANT>A565</PLANT>
<PLANT>A567</PLANT>
<PLANT>A585</PLANT>
<PLANT>A921</PLANT>
<PLANT>A938</PLANT>
</PLANT_GROUP>
</KPI_PLANT_GROUP_KEYWORD>
<MSU_CUSTOMERS/>
</DU>
<DU>
तो यह हल करने के लिए, मैं नीचे विधि का उपयोग करें और पूरी प्रतिक्रिया उससे संबंधित विराम चिह्न के साथ सभी विशेष वर्ण को बदलने के लिए गुजरती हैं।
private final static Hashtable htmlEntitiesTable = new Hashtable();
static {
htmlEntitiesTable.put("&","&");
htmlEntitiesTable.put(""","\"");
htmlEntitiesTable.put("<","<");
htmlEntitiesTable.put(">",">");
}
private String decodeHtmlEntityCharacters(String inputString) throws Exception {
Enumeration en = htmlEntitiesTable.keys();
while(en.hasMoreElements()){
String key = (String)en.nextElement();
String val = (String)htmlEntitiesTable.get(key);
inputString = inputString.replaceAll(key, val);
}
return inputString;
}
लेकिन एक और समस्या उत्पन्न हुई। प्रतिक्रिया इस सेगमेंट <VALUE>< 0.5 </VALUE<
होता है और इस विधि द्वारा मूल्यांकन किया जाएगा, तो उत्पादन होगा:
<VALUE>< 0.5</VALUE>
कौन सा XML अमान्य की संरचना फिर से बना देता है। डेटा सही और वैध है "< 0.5" लेकिन इसे VALUE तत्वों के भीतर रखना XML की संरचना पर समस्या का कारण बनता है।
क्या आप कृपया इस से निपटने में मदद कर सकते हैं? हो सकता है कि मुझे जिस तरह से प्रतिक्रिया मिलती है या उसका निर्माण किया जा सकता है। क्या वेब सेवा से प्रतिक्रिया पाने और प्रतिक्रिया प्राप्त करने का कोई बेहतर तरीका है?
मैं "<" या ">" वाले तत्वों से कैसे निपट सकता हूं?
ताकि आप अगर एक '<' डेटा या वाक्य रचना है पता लगाने के लिए कोई तरीका होना चाहिए? – Cruncher
मुझे सुझाव है कि VALUE पर एक विशेषता है। विशेषता को 1 या 0 बना दें, भले ही आपको कम/अधिक की आवश्यकता हो। (या -1, 0, 1 <, =, > के लिए) – Cruncher
<* सही * को एन्कोड क्यों नहीं करें? –