Java 如何在 Android 上调用 SOAP 网络服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/297586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:44:34  来源:igfitidea点击:

How to call a SOAP web service on Android

javaandroidweb-servicessoapwsdl

提问by BobbyShaftoe

I am having a lot of trouble finding good information on how to call a standard SOAP/WSDL web service with Android. All I've been able to find are either very convoluted documents and references to "kSoap2" and then some bit about parsing it all manually with SAX. OK, that's fine, but it's 2008, so I figured there should be some good library for calling standard web services.

我很难找到有关如何使用 Android 调用标准 SOAP/WSDL Web 服务的好信息。我所能找到的要么是非常复杂的文档和对“kSoap2”的引用,然后是一些关于使用SAX手动解析所有内容的内容。好的,那很好,但现在是 2008 年,所以我认为应该有一些很好的库来调用标准 Web 服务。

The web service is just basically one created in NetBeans. I would like to have IDE support for generating the plumbing classes. I just need the easiest/most-elegant way to contact a WSDLbased web service from an Android-based phone.

Web 服务基本上是在NetBeans 中创建的。我想要生成管道类的 IDE 支持。我只需要最简单/最优雅的方式从基于 Android 的手机联系基于WSDL的 Web 服务。

采纳答案by foxxtrot

Android does not provide any sort of SOAP library. You can either write your own, or use something like kSOAP 2. As you note, others have been able to compile and use kSOAP2 in their own projects, but I haven't had to.

Android 不提供任何类型的 SOAP 库。您可以自己编写,也可以使用kSOAP 2 之类的东西。正如您所注意到的,其他人已经能够在他们自己的项目中编译和使用 kSOAP2,但我不必这样做。

Google has shown, to date, little interest in adding a SOAP library to Android. My suspicion for this is that they'd rather support the current trends in Web Services toward REST-based services, and using JSON as a data encapsulation format. Or, using XMPP for messaging. But that is just conjecture.

迄今为止,Google 对将 SOAP 库添加到 Android 几乎没有兴趣。我对此的怀疑是,他们更愿意支持 Web 服务的当前趋势,即基于 REST 的服务,并使用 JSON 作为数据封装格式。或者,使用 XMPP 进行消息传递。但这只是猜测。

XML-based web services are a slightly non-trivial task on Android at this time. Not knowing NetBeans, I can't speak to the tools available there, but I agree that a better library should be available. It is possible that the XmlPullParser will save you from using SAX, but I don't know much about that.

目前,基于 XML 的 Web 服务在 Android 上是一项稍微重要的任务。不了解 NetBeans,我无法谈论那里可用的工具,但我同意应该提供更好的库。XmlPullParser 可能会使您免于使用 SAX,但我对此知之甚少。

回答by branchgabriel

I am sure you could make a little SOAP client with Axis. Axis installation instructions.

我相信您可以使用Axis制作一个小型 SOAP 客户端。轴安装说明

回答by Neil

org.apache.http.impl.client.DefaultHttpClientcomes in the Android SDK by default. That'll get you connected to the WSDL.

org.apache.http.impl.client.DefaultHttpClient默认情况下包含在 Android SDK 中。这将使您连接到 WSDL。

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
HttpResponse response = httpClient.execute(httpGet, localContext);

回答by jasonhudgins

SOAP is an ill-suited technology for use on Android (or mobile devices in general) because of the processing/parsing overhead that's required. A REST services is a lighter weight solution and that's what I would suggest. Android comes with a SAX parser, and it's fairly trivial to use. If you are absolutely required to handle/parse SOAP on a mobile device then I feel sorry for you, the best advice I can offer is just not to use SOAP.

由于所需的处理/解析开销,SOAP 是一种不适合在 Android(或一般的移动设备)上使用的技术。REST 服务是一种轻量级的解决方案,这就是我的建议。Android 带有一个 SAX 解析器,使用起来相当简单。如果您绝对需要在移动设备上处理/解析 SOAP,那么我为您感到抱歉,我能提供的最佳建议就是不要使用 SOAP。

回答by Viktor Bre?an

It's true that due to it's overhead SOAP is not the best choice for data exchange with mobile devices. However, you might find yourself in situation in which you do not control the format of server output.

确实,由于其开销,SOAP 不是与移动设备交换数据的最佳选择。但是,您可能会发现自己无法控制服务器输出的格式。

So, if you have to stick with SOAP, there is a kSOAP2 library patched for Android here:
http://code.google.com/p/ksoap2-android/

因此,如果您必须坚持使用 SOAP,这里有一个为 Android 打补丁的 kSOAP2 库:http:
//code.google.com/p/ksoap2-android/

回答by Samuh

I had my tryst with KSOAP; I chose a rather simpler approach.

我与 KSOAP 幽会;我选择了一种相当简单的方法。

Given a WSDL file, create SOAP Request templates for each Request(for e.g.: using SOAP UI) and then substitute the values to be passed in code. POST this data to the service end point using DefaultHttpClient instance and get the response stream. Parse the Response Stream using an XML Pull parser.

给定一个 WSDL 文件,为每个请求创建 SOAP 请求模板(例如:使用 SOAP UI),然后替换要在代码中传递的值。使用 DefaultHttpClient 实例将此数据发布到服务端点并获取响应流。使用 XML Pull 解析器解析响应流。

回答by Yves

If you can, go for JSON. Android comes with the complete org.json package

如果可以,请选择 JSON。Android 自带完整的 org.json 包

回答by Priyanjan

To call a web service from a mobile device (especially on an Android phone), I have used a very simple way to do it. I have not used any web service client API in attempt to call the web service. My approach is as follows to make a call.

要从移动设备(尤其是在 Android 手机上)调用 Web 服务,我使用了一种非常简单的方法。我没有使用任何 Web 服务客户端 API 来尝试调用 Web 服务。我的方法如下打电话。

  1. Create a simple HTTP connection by using the Java standard API HttpURLConnection.
  2. Form a SOAP request. (You can make help of SOAPUI to make a SOAP request.)
  3. Set doOutPut flag as true.
  4. Set HTTP header values like content-length, Content type, and User-agent. Do not forget to set Content-length value as it is a mandatory.
  5. Write entire the SOAP request to the output stream.
  6. Call the method to make a connection and receive the response (In my case I used getResonseCode).
  7. If your received response code as
    1. It means you are succeeded to call web service.
  8. Now take an input stream on the same HTTP connection and receive the string object. This string object is a SOAP response.
  9. If the response code is other than 200 then take a ErrorInputstream on same HTTPobject and receive the error if any.
  10. Parse the received response using SAXParser (in my case) or DOMParaser or any other parsing mechanism.
  1. 使用 Java 标准 API 创建一个简单的 HTTP 连接 HttpURLConnection
  2. 形成一个 SOAP 请求。(您可以借助 SOAPUI 来发出 SOAP 请求。)
  3. 将 doOutPut 标志设置为 true。
  4. 设置 HTTP 标头值,如内容长度、内容类型和用户代理。不要忘记设置 Content-length 值,因为它是强制性的。
  5. 将整个 SOAP 请求写入输出流。
  6. 调用该方法以建立连接并接收响应(在我的情况下,我使用了 getResonseCode)。
  7. 如果您收到的响应代码为
    1. 这意味着您成功调用了 Web 服务。
  8. 现在在同一个 HTTP 连接上获取输入流并接收字符串对象。这个字符串对象是一个 SOAP 响应。
  9. 如果响应代码不是 200,则ErrorInput在同一 HTTPobject 上获取流并接收错误(如果有)。
  10. 使用 SAXParser(在我的例子中)或 DOMParaser 或任何其他解析机制解析收到的响应。

I have implemented this procedure for the Android phone, and it is successfully running. I am able to parse the response even if it is more than 700 KB.

我已经在Android手机上实现了这个程序,并且运行成功。即使响应超过 700 KB,我也能解析它。

回答by Monte Chan

回答by Akash Kava

You can have a look at WSClient++

你可以看看WSClient++