如何在Android中以XML格式发送SOAP请求并解析SOAP响应?

发布于 2024-12-25 19:03:45 字数 2347 浏览 1 评论 0原文

我对 Android 应用程序开发非常陌生。在我的新 Android 应用程序中,我想显示来自网络服务的一些数据。这意味着我有一个 SOAP 消息,我需要解析来自 SOAP 响应的数据。在 iPhone 应用程序中,我非常了解如何解析 SOAP 消息响应,但是,在 Android 中,我不知道该怎么做?我在谷歌上搜索了很多并得到了一些想法。但是,我对此感到非常困惑。任何人都可以建议任何最简单的方法来理解 SOAP 发送请求/接收响应并在 Android 中解析(XML 格式SAXParser 中的响应吗?我已在我的项目中安装了 ksoap2-android- assembly-2.6.0-jar-with-dependency.jar 。在这里我找到了一些示例代码,我在这里发布,

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

这是正确的代码吗?请对我的问题提出任何建议。请帮我解决这个问题。提前致谢。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2025-01-01 19:03:45

Google for 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 解析器教程,仅使用 SAX,因为 android 不支持 STAX。为了发送 xml 请求,您可以将 xml 作为字符串发送,然后在服务器端进行解码。

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

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文