搭建了android环境,第一个需求是调用webservice, 看了一周的教程,至少有2种方法
实现1 用java直接socket编程 2调用封装好的ksoap2-android-assembly-2.4-jar-with-dependencies.jar
我用第2中方法实现了,步骤
1 下载ksoap2-android-assembly-2.4-jar-with-dependencies.jar, 并导入工程
右键工程 -> build path -〉add libraries
->user library -> user libraries -> new
起一个名字然后 add jars 导入ksoap2的jar
2 在AndroidManifest.xml的清单中加入<uses-permission android:name="android.permission.INTERNET" />
3 为了看到调试的信息 window->show view->logcat 如下图
4 写入如下代码,运行看Log
Log.v("Debug", "testest");
String nameSpace = "http://tempuri.org/";
String serviceURL = "http://192.168.1.116:12345/Service1.asmx";
String methodName = "HelloWorld";
String soapAction = "http://tempuri.org/HelloWorld";
SoapObject request = new SoapObject(nameSpace, methodName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet=true;
HttpTransportSE ht = new HttpTransportSE(serviceURL);
ht.debug = true;
try {
ht.call(soapAction, envelope);
if (envelope.getResponse() != null) {
Log.v("HttpRestonse", envelope.getResponse().toString());
} else {
Log.v("HttpRestonse", "NULL");
}
} catch (Exception e) {
e.printStackTrace();
Log.v("HttpRestonseErorr", e.toString());
}
log如下
02-19 01:08:19.238: VERBOSE/Debug(415): testest
02-19 01:08:19.517: VERBOSE/HttpRestonse(415): Hello World