如何发送SOAP请求和解析在Android的XML格式的SOAP响应? [英] How to send SOAP request and Parse SOAP response in XML format in Android?

查看:634
本文介绍了如何发送SOAP请求和解析在Android的XML格式的SOAP响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很新的Andr​​oid应用程序的开发。在我的新的Andr​​oid应用程序,我想表现出从web服务的一些数据。这意味着我有一个 SOAP消息,我需要从SOAP响应分析数据。在iPhone应用程序,我很清楚解析SOAP消息的反应,但在Android的我不知道如何做到这一点?我在谷歌搜索到很多东西,得到一些想法。但是,我很困惑这一点。任何人都可以请提出任何简单的方式了解SOAP发送请求/接收响应,并解析( XML格式)的响应的SAXParser 安卓?我已经安装了 ksoap2-机器人组装-2.6.0-JAR-与-dependencies.jar在我的项目。在这里,我发现了一些样品code,我在这里后,

Am very new to Android apps development. In my new Android app i want to show some data from webservice. This means i have a SOAP message, i need to parse the data from SOAP response. In iPhone app i knew very well to parse SOAP message response but, in android i don't know how to do this? I searched lot in Google and get some ideas. But, am very confusing on this. Can anyone please suggest any easiest way to understand SOAP send request/receive response and parse(XML format) the response in SAXParser in Android? I has installed ksoap2-android-assembly-2.6.0-jar-with-dependencies.jarin my project. Here i found some sample code, i post here,

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);
    }
 }

这是正确的code。请给我问题的任何建议。请帮我在这。先谢谢了。

Is this correct code. Please give any suggestion on my question. Please help me on this. Thanks in advance.

推荐答案


谷歌为Ksoap2教程U将得到他们中的很多。下面是示例code发送请求的Web服务。


Google for Ksoap2 tutorial u will get a lot of them . Here is sample code for sending request to web service .

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请检查教程,使用SAX而已,因为STAX不支持的Andr​​oid。对于发送XML请求ü可以发送XML作为字符串和SEVER端,然后去code。

And for xml pls check tutorial for xml parsers,use SAX only, as STAX is not supported in android . For sending xml request u can send xml as string and then decode on sever side .

这篇关于如何发送SOAP请求和解析在Android的XML格式的SOAP响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆