我很难找到有关如何使用Android调用标准SOAP / WSDL Web服务的良好信息。我能找到的所有文件都是非常复杂的文档和对“kSoap2”的引用,然后用SAX手动解析它。好的,这很好,但它是2008年,所以我认为应该有一些很好的库来调用标准的Web服务。
Web服务基本上是在NetBeans中创建的。我想有IDE支持生成管道类。我只需要最简单/最优雅的方式来联系基于Android手机的基于WSDL的网络服务。
答案 0 :(得分:242)
Android不提供任何类型的SOAP库。您可以自己编写,也可以使用kSOAP 2之类的内容。正如您所说,其他人已经能够在他们自己的项目中编译和使用kSOAP2,但我没有必要。
到目前为止,Google已经表明,对于向Android添加SOAP库几乎没有兴趣。我对此的怀疑是,他们宁愿支持当前基于REST服务的Web服务趋势,并使用JSON作为数据封装格式。或者,使用XMPP进行消息传递。但这只是推测。目前,基于XML的Web服务在Android上是一项稍微不重要的任务。不了解NetBeans,我不能说那里提供的工具,但我同意应该有更好的库。 XmlPullParser可能会使您无法使用SAX,但我对此并不了解。
答案 1 :(得分:98)
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);
答案 2 :(得分:58)
确实,由于它的开销,SOAP不是与移动设备进行数据交换的最佳选择。但是,您可能会发现自己处于无法控制服务器输出格式的情况。
所以,如果你必须坚持使用SOAP,那么在这里有一个为Android安装的kSOAP2库:
http://code.google.com/p/ksoap2-android/
答案 3 :(得分:30)
要从移动设备(特别是在Android手机上)调用网络服务,我使用了一种非常简单的方法。我没有使用任何Web服务客户端API来尝试调用Web服务。我的方法如下是打电话。
HttpURLConnection
。getResonseCode
)。ErrorInput
流
相同的HTTP对象并接收
错误,如果有的话。我已经为Android手机实施了此程序,并且已成功运行。即使响应超过700 KB,我也能够解析响应。
答案 4 :(得分:29)
SOAP是一种不适合在Android(或一般移动设备)上使用的技术,因为它需要处理/解析开销。 REST服务是一种更轻量级的解决方案,这就是我的建议。 Android附带一个SAX解析器,使用起来相当简单。如果您绝对需要在移动设备上处理/解析SOAP,那么我为您感到遗憾,我能提供的最佳建议就是不使用SOAP。
答案 5 :(得分:23)
请勿忘记在项目中添加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);
}
}
}
答案 6 :(得分:23)
大约一年前,我正在阅读这个线程,试图弄清楚如何在Android上进行SOAP调用 - 使用HttpClient构建我自己的建议导致我为Android构建自己的SOAP库:
基本上它允许你构建信封以通过简单的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”时偶然发现这个帖子可以给它带来一些好处。
答案 7 :(得分:20)
我跟KSOAP约会;我选择了一种相当简单的方法。
给定WSDL文件,为每个请求创建SOAP请求模板(例如:使用SOAP UI),然后替换要在代码中传递的值。使用DefaultHttpClient实例将此数据发布到服务端点并获取响应流。使用XML Pull解析器解析响应流。
答案 8 :(得分:16)
您可以查看WSClient++
答案 9 :(得分:15)
我为Android平台创建了一个新的SOAP客户端。它使用的是JAX-WS生成的接口,但到目前为止它只是一个概念验证。
如果您有兴趣,请尝试示例和/或在AndroidSOAP观看来源。
答案 10 :(得分:14)
如果可以,请选择JSON。 Android附带完整的org.json包
答案 11 :(得分:13)
调用 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();
答案 12 :(得分:12)
我希望 Calling a web service from Android 有所帮助。
答案 13 :(得分:9)
我相信你可以使用Axis创建一个小型SOAP客户端。 Axis installation instructions
答案 14 :(得分:9)
几个月前,我在j2ee应用程序中使用jax-ws Web服务,我们使用CXF wsdl2java从WSDL文件生成WS客户端存根,并使用这些客户端存根来使用Web服务。几周前,当我试图以相同的方式在Android平台上使用Web服务时,我无法做到,因为android jar并不是所有的&#34; jax-ws&#34;支持其中的课程。那段时间我没有找到任何这样的工具(如果我没有有效地谷歌),以满足我的要求 -
所以,我开发了自己的Android SOAP Client Generation Tool。你必须遵循以下步骤:
例如:
ComplexOperationService service = new ComplexOperationService( );
ComplexOperation port= service.getComplexOperationPort();
SomeComplexRequest request = --Get some complex request----;
SomeComplexResp resp = port.operate( request );
答案 15 :(得分:8)
我认为 Call SOAP Web Service from Android application 会对你有所帮助。
答案 16 :(得分:6)
如果您可以使用JSON,则 Developing Application Services with PHP Servers and Android Phone Clients 中有白皮书,视频和sample.code。
答案 17 :(得分:6)
按照SOAP
方法执行这些步骤来自WSDL文件,
为每个请求创建SOAP请求模板。
然后替换要在代码中传递的值。
使用DefaultHttpClient实例将此数据发布到服务端点。
获取响应流,最后
使用XML Pull解析器解析响应流。
答案 18 :(得分:6)
答案 19 :(得分:5)
我建议查看一个非常有用的工具,它对我帮助很大。伙计们 谁也照顾这个项目非常有帮助。 www.wsdl2code.com /
答案 20 :(得分:4)
如果您遇到有关在Android中调用Web Service的问题 您可以使用以下代码来调用Web服务并获得响应。确保您的Web服务以数据表格式返回响应。如果您使用 SQL Server 数据库中的数据,此代码将对您有所帮助。如果你使用 MYSQL ,你需要改变一件事,只需用 DocumentElement
替换句子obj2=(SoapObject) obj1.getProperty("NewDataSet");
中的单词 NewDataSet
void callWebService(){
private static final String NAMESPACE = "http://tempuri.org/"; // for wsdl it may be package name i.e http://package_name
private static final String URL = "http://localhost/sample/services/MyService?wsdl";
// you can use IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = "urn:" + METHOD_NAME;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("parm_name", prm_value);// Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;// **If your Webservice in .net otherwise remove it**
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service
// Method
} catch (Exception e) {
e.printStackTrace();
}
// Next task is to get Response and format that response
SoapObject obj, obj1, obj2, obj3;
obj = (SoapObject) envelope.getResponse();
obj1 = (SoapObject) obj.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
for (int i = 0; i < obj2.getPropertyCount(); i++) {
// the method getPropertyCount() and return the number of rows
obj3 = (SoapObject) obj2.getProperty(i);
obj3.getProperty(0).toString();// value of column 1
obj3.getProperty(1).toString();// value of column 2
// like that you will get value from each column
}
}
如果您对此有任何疑问,可以写信给我。
答案 21 :(得分:3)
这是在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();
}
}
答案 22 :(得分:3)
请下载并在项目中添加SOAP库文件 文件名: ksoap2-android-assembly-3.4.0-jar-with-dependencies
清理应用程序然后启动程序
以下是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 Or String
为了更好的参考, https://trinitytuts.com/load-data-from-soap-web-service-in-android-application/
感谢。
答案 23 :(得分:2)
要从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;
}
}
答案 24 :(得分:2)
您可以使用某些标题通过http发布肥皂通话。 我没有像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;
}
答案 25 :(得分:1)
添加Soap Libaray(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;
}
}