如果可以,请选择JSON。 Android附带完整的org.json包
SOAP是一种不适合在Android(或一般移动设备)上使用的技术,因为它需要处理/解析开销。 REST服务是一种更轻量级的解决方案,这就是我的建议。 Android附带一个SAX解析器,使用起来相当简单。如果您绝对需要在移动设备上处理/解析SOAP,那么我为您感到遗憾,我能提供的最佳建议就是不使用SOAP。
对我来说,最简单的方法是使用好的工具来生成所有必需的类。我个人使用这个网站:
http://easywsdl.com/
它支持相当复杂的Web服务并使用ksoap2。
Android不提供任何类型的SOAP库。你可以自己编写,也可以使用类似的东西 kSOAP 2 。正如您所说,其他人已经能够在他们自己的项目中编译和使用kSOAP2,但我没有必要。
到目前为止,谷歌对向Android添加SOAP库几乎没有兴趣。我对此的怀疑是,他们宁愿支持当前基于REST服务的Web服务趋势,并使用JSON作为数据封装格式。或者,使用XMPP进行消息传递。但这只是猜想。
基于XML的Web服务目前在Android上是一项稍微不重要的任务。不了解NetBeans,我不能说那里提供的工具,但我同意应该有更好的库。 XmlPullParser可能会使您无法使用SAX,但我对此并不了解。
您可以通过http使用某些标题执行soap调用。 我没有像ksoap2这样的额外库就解决了这个问题 这是从肥皂服务获取订单的实时代码
private static HashMap<String,String> mHeaders = new HashMap<>(); static { mHeaders.put("Accept-Encoding","gzip,deflate"); mHeaders.put("Content-Type", "application/soap+xml"); mHeaders.put("Host", "35.15.85.55:8080"); mHeaders.put("Connection", "Keep-Alive"); mHeaders.put("User-Agent","AndroidApp"); mHeaders.put("Authorization","Basic Q2xpZW50NTkzMzppMjR3s2U="); // optional }public final static InputStream receiveCurrentShipments(String stringUrlShipments) { int status=0; String xmlstring= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ser=\"http://35.15.85.55:8080/ServiceTransfer\">\n" + " <soap:Header/>\n" + " <soap:Body>\n" + " <ser:GetAllOrdersOfShipment>\n" + " <ser:CodeOfBranch></ser:CodeOfBranch>\n" + " </ser:GetAllOrdersOfShipment>\n" + " </soap:Body>\n" + "</soap:Envelope>"; StringBuffer chaine = new StringBuffer(""); HttpURLConnection connection = null; try { URL url = new URL(stringUrlShipments); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-Length", xmlstring.getBytes().length + ""); connection.setRequestProperty("SOAPAction", "http://35.15.85.55:8080/ServiceTransfer/GetAllOrdersOfShipment"); for(Map.Entry<String, String> entry : mHeaders.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); connection.setRequestProperty(key,value); } connection.setRequestMethod("POST"); connection.setDoInput(true); OutputStream outputStream = connection.getOutputStream(); outputStream.write(xmlstring.getBytes("UTF-8")); outputStream.close(); connection.connect(); status = connection.getResponseCode(); } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { Log.i("HTTP Client", "HTTP status code : " + status); } InputStream inputStream = null; try { inputStream = connection.getInputStream(); } catch (IOException e) { e.printStackTrace(); } return inputStream; }
我建议查看一个非常有用的工具,帮助了我很多。伙计们 谁也照顾这个项目非常有帮助。 www.wsdl2code.com/
你可以看看 WSClient ++
要从android调用SOAP Web服务,请尝试使用此客户端
不要忘记在你的java构建路径中添加ksoap2-android.jar
public class WsClient { private static final String SOAP_ACTION = "somme"; private static final String OPERATION_NAME = "somme"; private static final String WSDL_TARGET_NAMESPACE = "http://example.ws"; private static final String SOAP_ADDRESS = "http://192.168.1.2:8080/axis2/services/Calculatrice?wsdl"; public String caclculerSomme() { String res = null; SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME); request.addProperty("a", "5"); request.addProperty("b", "2"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); try { httpTransport.call(SOAP_ACTION, envelope); String result = envelope.getResponse().toString(); res = result; System.out.println("############# resull is :" + result); } catch (Exception exception) { System.out.println("########### ERRER" + exception.getMessage()); } return res; } }
添加Soap Libaray( ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar ):
ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar
public static String Fn_Confirm_CollectMoney_Approval(
HashMap < String, String > str1, HashMap < String, String > str2, HashMap < String, String > str3) { Object response = null; String METHOD_NAME = "CollectMoney"; String NAMESPACE = "http://xxx/yyy/xxx"; String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; String SOAP_ACTION = ""; try { SoapObject RequestParent = new SoapObject(NAMESPACE, METHOD_NAME); SoapObject Request1 = new SoapObject(NAMESPACE, "req"); PropertyInfo pi = new PropertyInfo(); Set mapSet1 = (Set) str1.entrySet(); Iterator mapIterator1 = mapSet1.iterator(); while (mapIterator1.hasNext()) { Map.Entry mapEntry = (Map.Entry) mapIterator1.next(); String keyValue = (String) mapEntry.getKey(); String value = (String) mapEntry.getValue(); pi = new PropertyInfo(); pi.setNamespace("java:com.xxx"); pi.setName(keyValue); pi.setValue(value); Request1.addProperty(pi); } mapSet1 = (Set) str3.entrySet(); mapIterator1 = mapSet1.iterator(); while (mapIterator1.hasNext()) { Map.Entry mapEntry = (Map.Entry) mapIterator1.next(); // getKey Method of HashMap access a key of map String keyValue = (String) mapEntry.getKey(); // getValue method returns corresponding key's value String value = (String) mapEntry.getValue(); pi = new PropertyInfo(); pi.setNamespace("java:com.xxx"); pi.setName(keyValue); pi.setValue(value); Request1.addProperty(pi); } SoapObject HeaderRequest = new SoapObject(NAMESPACE, "XXX"); Set mapSet = (Set) str2.entrySet(); Iterator mapIterator = mapSet.iterator(); while (mapIterator.hasNext()) { Map.Entry mapEntry = (Map.Entry) mapIterator.next(); // getKey Method of HashMap access a key of map String keyValue = (String) mapEntry.getKey(); // getValue method returns corresponding key's value String value = (String) mapEntry.getValue(); pi = new PropertyInfo(); pi.setNamespace("java:com.xxx"); pi.setName(keyValue); pi.setValue(value); HeaderRequest.addProperty(pi); } Request1.addSoapObject(HeaderRequest); RequestParent.addSoapObject(Request1); SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER10); soapEnvelope.dotNet = false; soapEnvelope.setOutputSoapObject(RequestParent); HttpTransportSE transport = new HttpTransportSE(URL, 120000); transport.debug = true; transport.call(SOAP_ACTION, soapEnvelope); response = (Object) soapEnvelope.getResponse(); int cols = ((SoapObject) response).getPropertyCount(); Object objectResponse = (Object) ((SoapObject) response) .getProperty("Resp"); SoapObject subObject_Resp = (SoapObject) objectResponse; modelObject = new ResposeXmlModel(); String MsgId = subObject_Resp.getProperty("MsgId").toString(); modelObject.setMsgId(MsgId); String OrgId = subObject_Resp.getProperty("OrgId").toString(); modelObject.setOrgId(OrgId); String ResCode = subObject_Resp.getProperty("ResCode").toString(); modelObject.setResCode(ResCode); String ResDesc = subObject_Resp.getProperty("ResDesc").toString(); modelObject.setResDesc(ResDesc); String TimeStamp = subObject_Resp.getProperty("TimeStamp") .toString(); modelObject.setTimestamp(ResDesc); return response.toString(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
我跟KSOAP约会了;我选择了一种相当简单的方法。
给定WSDL文件,为每个请求创建SOAP请求模板(例如:使用SOAP UI),然后替换要在代码中传递的值。使用DefaultHttpClient实例将此数据发布到服务端点并获取响应流。使用XML Pull解析器解析响应流。
请下载并在项目中添加SOAP库文件 文件名 : 的 ksoap2-机器人组装-3.4.0-JAR-与依赖性 强>
清理应用程序,然后启动程序
这是SOAP服务调用的代码
String SOAP_ACTION = "YOUR_ACTION_NAME"; String METHOD_NAME = "YOUR_METHOD_NAME"; String NAMESPACE = "YOUR_NAME_SPACE"; String URL = "YOUR_URL"; SoapPrimitive resultString = null; try { SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); addPropertyForSOAP(Request); SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); soapEnvelope.dotNet = true; soapEnvelope.setOutputSoapObject(Request); HttpTransportSE transport = new HttpTransportSE(URL); transport.call(SOAP_ACTION, soapEnvelope); resultString = (SoapPrimitive) soapEnvelope.getResponse(); Log.i("SOAP Result", "Result Celsius: " + resultString); } catch (Exception ex) { Log.e("SOAP Result", "Error: " + ex.getMessage()); } if(resultString != null) { return resultString.toString(); } else{ return "error"; }
结果可能是JSONObject或JSONArray或String
为了更好的参考, https://trinitytuts.com/load-data-from-soap-web-service-in-android-application/
谢谢。
呼叫 的 ksoap2 强> 方法。它工作得非常好。
设置细节,比如
private static String mNAMESPACE=null; private static String mURL=null; public static Context context=null; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(Request); envelope.addMapping(mNAMESPACE, "UserCredentials",new UserCredendtials().getClass()); AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(mURL);
然后得到结果呢
androidHttpTransport.call(SOAP_ACTION, envelope); result = (SoapPrimitive)envelope.getResponse();
这是在android中使用SOAP Web服务的一个工作示例。
**注意:: ***不要忘记在项目中添加ksoap2.jar,并在AndroidManifest文件中添加INTERNET权限*
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; public final String METHOD_NAME = "FahrenheitToCelsius"; public final String PROPERTY_NAME = "Fahrenheit"; public final String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius"; public final String SOAP_ADDRESS = "http://www.w3schools.com/webservices/tempconvert.asmx"; private class TestAsynk extends AsyncTask<String, Void, String> { @Override protected void onPostExecute(String result) { super.onPostExecute(result); Toast.makeText(getApplicationContext(), String.format("%.2f", Float.parseFloat(result)), Toast.LENGTH_SHORT).show(); } @Override protected String doInBackground(String... params) { SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, METHOD_NAME); request.addProperty(PROPERTY_NAME, params[0]); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE( SOAP_ADDRESS); Object response = null; try { androidHttpTransport.call(SOAP_ACTION, envelope); response = envelope.getResponse(); Log.e("Object response", response.toString()); } catch (Exception e) { e.printStackTrace(); } return response.toString(); } }
大约一年前,我正在阅读这个线程,试图找出如何在Android上进行SOAP调用 - 使用HttpClient构建我自己的建议导致我为Android构建自己的SOAP库:
IceSoap
基本上,它允许您构建信封以通过简单的Java API发送,然后自动将它们解析为您通过XPath定义的对象...例如:
<Dictionary> <Id></Id> <Name></Name> </Dictionary>
变为:
@XMLObject("//Dictionary") public class Dictionary { @XMLField("Id") private String id; @XMLField("Name") private String name; }
我正在将它用于我自己的项目,但我认为它可能会帮助其他人,所以我花了一些时间把它分开并记录下来。我真的很喜欢它,如果你的一些可怜的灵魂在谷歌搜索“SOAP Android”时偶然发现这个线程可能会给它带来一些好处。
org.apache.http.impl.client.DefaultHttpClient 默认情况下会出现在Android SDK中。那将让你连接到WSDL。
org.apache.http.impl.client.DefaultHttpClient
HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.example.com/" + URL); HttpResponse response = httpClient.execute(httpGet, localContext);
不要忘记在项目中添加ksoap2.jar并添加 AndroidManifest文件中的INTERNET权限
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class WebserviceActivity extends Activity { private static final String NAMESPACE = "https://api.authorize.net/soap/v1/"; private static final String URL ="https://apitest.authorize.net/soap/v1/Service.asmx?wsdl"; private static final String SOAP_ACTION = "https://api.authorize.net/soap/v1/AuthenticateTest"; private static final String METHOD_NAME = "AuthenticateTest"; private TextView lblResult; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lblResult = (TextView) findViewById(R.id.tv); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("name","44vmMAYrhjfhj66fhJN"); request.addProperty("transactionKey","9MDQ7fghjghjh53H48k7e7n"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION, envelope); //SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse(); // SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse(); SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; lblResult.setText(resultsRequestSOAP.toString()); System.out.println("Response::"+resultsRequestSOAP.toString()); } catch (Exception e) { System.out.println("Error"+e); } } }
按照SOAP方法执行这些步骤
从WSDL文件中
为每个请求创建SOAP请求模板。
然后替换要在代码中传递的值。
使用DefaultHttpClient实例将此数据发布到服务端点。
最后得到响应流
使用XML Pull解析器解析响应流。
几个月前我在j2ee应用程序中使用jax-ws web服务,我们正在使用 CXF wsdl2java 从WSDL文件生成WS客户端存根,并使用这些客户端存根我们使用Web服务。几周前,当我试图以相同的方式在Android平台上使用Web服务时,我无法,因为android jar并没有所有“jax-ws”支持类。那个时候我没有找到任何这样的工具(如果我没有有效地谷歌),以满足我的要求 -
所以,我开发了自己的 Android SOAP客户端生成工具 。你必须遵循以下步骤:
例如:
ComplexOperationService service = new ComplexOperationService( ); ComplexOperation port= service.getComplexOperationPort(); SomeComplexRequest request = --Get some complex request----; SomeComplexResp resp = port.operate( request );
我认为 从Android应用程序调用SOAP Web服务 会帮助你很多。