PHP实现soap null wsdl服务端,android 实现android client访问
利用php实现soap的服务端供app等客户端访问
服务端:
新建php文件testServer.php
<?php
class service
{
public function HelloWorld()
{
return "hello";
}
}
$s = new SoapServer(null, array('uri' => 'test'));
$s->setClass("service");
$s->handle();
?>
客户端:
PHP调用:
import urllib2
SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
</soap:Header>
<soap:Body>
<HelloWorld>
</HelloWorld>
</soap:Body>
</soap:Envelope>"""
url = "http://www.qs77.net/testServer.php"
http_headers = {
"Accept": "application/soap+xml,multipart/related,text/*",
"Cache-Control":"no-cache",
"Content-Type": "text/xml; charset=utf-8"
}
request = urllib2.Request(url, SM_TEMPLATE, http_headers)
response = urllib2.urlopen(request)
print response.read()
android调用:
new Thread(new Runnable() {
@Override public void run() {
try {
final String soaptemp = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" + " <soap:Header>" + " </soap:Header>" + " <soap:Body>" + " <HelloWorld>" + " </HelloWorld>" + " </soap:Body>" + "</soap:Envelope>";
URL url = new URL("http://www.qs77.net/testService.php"); HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); urlCon.setUseCaches(false); urlCon.setRequestProperty("Accept", "application/soap+xml,multipart/related,text/*"); urlCon.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); urlCon.setRequestProperty("Content-Length", "" + soaptemp.getBytes().length); urlCon.setRequestProperty("soapAction", "HelloWorld"); urlCon.setRequestMethod("POST"); urlCon.setDoOutput(true); urlCon.setDoInput(true); OutputStream out = urlCon.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(soaptemp); writer.flush(); writer.close();
int status = urlCon.getResponseCode(); WolfLog.e(TAG, "" + status); InputStream in = null; if (status == 200) { in = urlCon.getInputStream(); } else { in = urlCon.getErrorStream(); } BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); String inputLine; StringBuilder input = new StringBuilder();
while ((inputLine = reader.readLine()) != null) { input.append(inputLine); } Document document = DocumentHelper.parseText(input.toString()); Element elementRoot = document.getRootElement(); Iterator<Element> iter = elementRoot.elementIterator();
while (iter.hasNext()) {
Element element = (Element)iter.next();
if ("Body".equals(element.getName())) { Iterator<Element> innerIter = element.elementIterator();
while (innerIter.hasNext()) { Element innerElement = (Element)innerIter.next(); WolfLog.e(TAG, innerElement.getName());
if ("HelloWorldResponse".equals(innerElement.getName())) { Iterator<Element> iinnerIter = innerElement.elementIterator();
while(iinnerIter.hasNext()) {
Element iinnerElement = (Element) iinnerIter.next();
if ("return".equals(iinnerElement.getName())) { Message msg = mHandler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("msg", iinnerElement.getText()); msg.setData(bundle);
mHandler.sendMessage(msg);
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
发表评论: