我有这个表示XML的字符串:
String soapCall="<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:enCodingStyle="http://www.w3.org/2001/12/soap-enCoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:addDownloadParams> <m:IDApp>";soapCall+=IDApp;soapCall+="<m:versionApp>";soapCall+=versonApp;soapCall+="</m:versionApp> <m:os>AndroID</m:os> </m:addDownloadParams> </soap:Body> </soap:Envelope>";
我有这个soap webservice:
http://stats.mywebsite.com/ws/adddownload
现在,我需要将该字符串传递给androID上的soap webservice,但我不知道方式,我知道我需要使用httpclIEnte:
httpClIEnt httpClIEnt = new DefaulthttpClIEnt();httpContext localContext = new BasichttpContext();
但我不知道如何用该字符串调用soapwebservice.
有人有代码示例吗?我在谷歌上找不到它.我不想使用图书馆,我需要自己做
谢谢
编辑:这是现在的代码,但它无法正常工作,我得到错误500内部服务器错误:
public static byte[] addDownloadIntoServer(){ byte[] result = null; String SERVER_URL="http://stats.mywebsite.com/ws/server.PHP"; String SOAP_ACTION="addDownload"; String body="<?xml version=\"1.0\" enCoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://stats.mobincube.com/\"><SOAP-ENV:Body><ns1:addDownloadParams>"; body+="<IDApp>" +SectionManager.instance.app_ID+"</IDApp>"; body+="<versionApp>"+SectionManager.instance.app_version+"</versionApp>"; body+="<source>androID</source> <os>AndroID</os> </ns1:addDownloadParams></SOAP-ENV:Body></SOAP-ENV:Envelope>"; httpParams httpParameters = new BasichttpParams(); // Set the timeout in milliseconds until a connection is established. int timeoutConnection = 15000; httpconnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 35000; httpconnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaulthttpClIEnt httpclIEnt = new DefaulthttpClIEnt(httpParameters); /* * httpclIEnt.getCredentialsProvIDer().setCredentials( new * AuthScope("os.icloud.com", 80, null, "Digest"), new * UsernamePasswordCredentials(username, password)); */ httpPost httppost = new httpPost(SERVER_URL ); httppost.setheader("soapaction", SOAP_ACTION); httppost.setheader("Content-Type", "text/xml; charset=utf-8"); System.out.println("executing request" + httppost.getRequestline()); //Now create a soap request message as follows: final StringBuffer soap = new StringBuffer(); soap.append("\n"); soap.append(""); // this is a sample data..you have create your own required data BEGIN soap.append(" \n"); soap.append(" \n"); soap.append("" + body); soap.append(" \n"); soap.append(" \n"); /* soap.append(body); */ // END of MEssage Body soap.append(""); Log.i("SOAP Request", ""+soap.toString()); // END of full SOAP request message try { httpentity entity = new StringEntity(soap.toString(),http.UTF_8); httppost.setEntity(entity); httpResponse response = httpclIEnt.execute(httppost);// calling server httpentity r_entity = response.getEntity(); //get response Log.i("Reponse header", "Begin..."); // response headers Log.i("Reponse header", "Statusline:"+response.getStatusline()); header[] headers = response.getAllheaders(); for(header h:headers) Log.i("Reponse header",h.getname() + ": " + h.getValue()); Log.i("Reponse header", "END..."); if (r_entity != null) { result = new byte[(int) r_entity.getContentLength()]; if (r_entity.isstreaming()) { DatainputStream is = new DatainputStream( r_entity.getContent()); is.readFully(result); } } }catch (Exception E) { Log.i("Exception While Connecting", ""+E.getMessage()); E.printstacktrace(); } httpclIEnt.getConnectionManager().shutdown(); //shut down the connection return result; }
解决方法:
解:
private byte[] callSOAPServer() { byte[] result = null; httpParams httpParameters = new BasichttpParams(); // Set the timeout in milliseconds until a connection is established. int timeoutConnection = 15000; httpconnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 35000; httpconnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaulthttpClIEnt httpclIEnt = new DefaulthttpClIEnt(httpParameters); /* * httpclIEnt.getCredentialsProvIDer().setCredentials( new * AuthScope("os.icloud.com", 80, null, "Digest"), new * UsernamePasswordCredentials(username, password)); */ httpPost httppost = new httpPost(SERVER_URL ); httppost.setheader("soapaction", SOAP_ACTION); httppost.setheader("Content-Type", "text/xml; charset=utf-8"); System.out.println("executing request" + httppost.getRequestline()); //Now create a soap request message as follows: final StringBuffer soap = new StringBuffer(); soap.append("\n"); soap.append(""); // this is a sample data..you have create your own required data BEGIN soap.append(" \n"); soap.append(" \n"); soap.append("" + body); soap.append(" \n"); soap.append(" \n"); /* soap.append(body); */ // END of MEssage Body soap.append(""); Log.i("SOAP Request", ""+soap.toString()); // END of full SOAP request message try { httpentity entity = new StringEntity(soap.toString(),http.UTF_8); httppost.setEntity(entity); httpResponse response = httpclIEnt.execute(httppost);// calling server httpentity r_entity = response.getEntity(); //get response Log.i("Reponse header", "Begin..."); // response headers Log.i("Reponse header", "Statusline:"+response.getStatusline()); header[] headers = response.getAllheaders(); for(header h:headers){ Log.i("Reponse header",h.getname() + ": " + h.getValue()); } Log.i("Reponse header", "END..."); if (r_entity != null) { result = new byte[(int) r_entity.getContentLength()]; if (r_entity.isstreaming()) { DatainputStream is = new DatainputStream( r_entity.getContent()); is.readFully(result); } } } catch (Exception E) { Log.i("Exception While Connecting", ""+E.getMessage()); E.printstacktrace(); } httpclIEnt.getConnectionManager().shutdown(); //shut down the connection return result; }
2)你必须解析上面函数返回的byteArray的输出.例如:
byte[] initReqrepsonse = callSOAPServer(soapBodymessage );ByteArrayinputStream bais=new ByteArrayinputStream(initReqrepsonse);// Now parse the xml as/** Handling XML */SAXParserFactory spf = SAXParserFactory.newInstance();SAXParser sp = spf.newSAXParser();XMLReader xr = sp.getXMLReader();/** Create handler to handle XML Tags ( extends DefaultHandler ) */// ResponseParser is XML parser class which will parse the XML output.ResponseParser myXMLHandler = new ResponseParser();xr.setContentHandler(myXMLHandler);Log.i("XML data", bais.toString());xr.parse(new inputSource(bais));
这样,您可以在不使用第三方库的情况下访问任何SOAP Web服务方法.
如果需要更正,请告诉我.
以上是内存溢出为你收集整理的如何使用简单的String(字符串格式的xml)调用SOAP Web服务全部内容,希望文章能够帮你解决如何使用简单的String(字符串格式的xml)调用SOAP Web服务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)