如何在Android中以XML格式发送SOAP请求和解析SOAP响应?
问题描述:
Android应用程序开发非常新。在我的新Android应用程序中,我想显示来自webservice的一些数据。这意味着我有一个SOAP message
,我需要解析来自SOAP响应的数据。在iPhone应用程序中,我很清楚解析SOAP消息响应,但在Android中,我不知道如何执行此操作?我在谷歌搜索很多,并得到一些想法。但是,这很令人困惑。任何人都可以请建议任何最简单的方法来理解SOAP发送请求/接收响应和解析(XML format
)在SAXParser
在Android
响应?我在我的项目中安装了ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar
。在这里,我发现了一些示例代码,我在这里发布,如何在Android中以XML格式发送SOAP请求和解析SOAP响应?
import java.io.*;
import org.ksoap2.SoapEnvelope;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;
public class ParsingSteps
{
public static void main(String[] args)
{
try{
// String msg="<hello>World!</hello>";
String msg = "<SOAP-ENV:Envelope " + "xmlns:SOAP-ENV=\"http://
www.w3.org/2001/12/soap-envelope\" " + "xmlns:xsi=\"http://www.w3.org/
2001/XMLSchema-instance <http://www.w3.org/%0A2001/XMLSchema-instance>\""
+"xmlns:xsd=\"http://www.w3.org/2001/
XMLSchema\"& gt;" +
"<SOAP-ENV:Body>" +
"<result>" +
"<message xsi:type=\"xsd:string\">Hello World</message>" +
"</result>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
// byte[] in= msg.getBytes();
KXmlParser parser=new KXmlParser();
parser.setInput(new StringReader(msg));
SoapEnvelope soapenvelope= new SoapEnvelope
(SoapEnvelope.VER12);
//soapenvelope.parse(parser);
soapenvelope.parseBody(parser);
}
catch (IOException e) {
System.out.println("Error reading URI: " + e.getMessage());
} catch (XmlPullParserException e) {
System.out.println("Error in parsing: " + e.getMessage());
}
// String result=parser.getName();
//System.out.println(result);
}
}
这是正确的代码。请对我的问题提出任何建议。请帮助我。提前致谢。
答
谷歌对于Ksoap2教程你会得到很多。以下是将请求发送到Web服务的示例代码。
public class WebServicePoc extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/Arnoid";
private static final String METHOD_NAME = "Arnoid";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://ipaddress:port/UserAuthenticationInterfacer.asmx";
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HashMap<String, String> a=new HashMap<String, String>();
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("FOSID", "1994");
request.addProperty("IMEINumber", "");
request.addProperty("SIMCardNo", "");
request.addProperty("ApplicationName", "App");
request.addProperty("CurrentVersion", "1.0.0.0");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
editText=(EditText)findViewById(R.id.text1);
editText.setText(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
而对于xml请为xml解析器检查教程,因为在android中不支持STAX,所以请仅使用SAX。对于发送XML请求,你可以将xml作为字符串发送,然后在服务器端进行解码。
非常感谢您的回复。我将把你的想法融入到我的项目中。我希望你的帮助在将来。谢谢。 – Gopinath 2012-01-07 06:05:44
@Gopinath如何设置属性以发送正确的消息? – arniotaki 2014-10-10 13:40:34