Android 调用WCF实例html
1. 构建服务端程序java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using System.ServiceModel;
namespace yournamespace
{
public
interface
IHello
{
[OperationContract]
string SayHello();
}
}
<br>
|
1
2
3
4
5
6
7
8
9
10
|
namespace YourNameSpace
{
public
class
YourService
{
public
string SayHello(string words)
{
return
"Hello "
+ words;
}
}
}
|
2. 构建IIS网站宿主android
YourService.svcgit
<%@ServiceHost Debug="true" Service="YourNameSpace.YourService"%>github
Web.configweb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<configuration>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations >
<add relativeAddress=
"YourService.svc"
service=
"YourNameSpace.YourService"
/>
</serviceActivations >
</serviceHostingEnvironment >
<bindings>
<basicHttpBinding>
<binding name=
"BasicHttpBindingCfg"
closeTimeout=
"00:01:00"
openTimeout=
"00:01:00"
receiveTimeout=
"00:10:00"
sendTimeout=
"00:01:00"
bypassProxyOnLocal=
"false"
hostNameComparisonMode=
"StrongWildcard"
maxBufferPoolSize=
"524288"
maxReceivedMessageSize=
"2147483647"
messageEncoding=
"Text"
textEncoding=
"utf-8"
useDefaultWebProxy=
"true"
allowCookies=
"false"
>
<readerQuotas maxDepth=
"32"
maxStringContentLength=
"8192"
maxArrayLength=
"16384"
maxBytesPerRead=
"4096"
maxNameTableCharCount=
"16384"
/>
<security mode=
"None"
>
<transport clientCredentialType=
"None"
proxyCredentialType=
"None"
realm=
""
/>
<message clientCredentialType=
"UserName"
algorithmSuite=
"Default"
/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name=
"YourNameSpace.YourService"
behaviorConfiguration=
"ServiceBehavior"
>
<host>
<baseAddresses>
</baseAddresses>
</host>
<endpoint binding=
"basicHttpBinding"
contract=
"YourNameSpace.你的服务契约接口"
>
<identity>
<dns value=
"localhost"
/>
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=
"ServiceBehavior"
>
<serviceMetadata httpGetEnabled=
"true"
/>
<serviceDebug includeExceptionDetailInFaults=
"true"
/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug=
"true"
/>
</system.web>
</configuration>
|
3. 寄宿服务服务器
把网站发布到web服务器, 指定网站虚拟目录指向该目录app
若是你可以访问http://你的IP:端口/虚拟目录/服务.svcide
那么,恭喜你,你的服务端成功了! 工具
4. 使用ksoap2调用WCF
去ksoap2官网
http://code.google.com/p/ksoap2-android/ 下载最新jar
5. 在Eclipse中新建一个Java项目,测试你的服务
新建一个接口, 用于专门读取WCF返回的SoapObject对象
ISoapService
1
2
3
4
5
6
7
8
9
|
package
junit.soap.wcf;
import
org.ksoap2.serialization.SoapObject;
public
interface
ISoapService {
SoapObject LoadResult();
}
<br>
|
HelloService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package
junit.soap.wcf;
import
java.io.IOException;
import
org.ksoap2.SoapEnvelope;
import
org.ksoap2.serialization.SoapObject;
import
org.ksoap2.serialization.SoapSerializationEnvelope;
import
org.ksoap2.transport.HttpTransportSE;
import
org.xmlpull.v1.XmlPullParserException;
public
class
HelloService
implements
ISoapService {
private
static
final
String MethodName =
"SayHello"
;
private
String words;
public
HelloService(String words) {
this
.words = words;
}
public
SoapObject LoadResult() {
SoapObject soapObject =
new
SoapObject(NameSpace, MethodName);
soapObject.addProperty(
"words"
, words);
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(SoapEnvelope.VER11);
// 版本
envelope.bodyOut = soapObject;
envelope.dotNet =
true
;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE trans =
new
HttpTransportSE(URL);
trans.debug =
true
;
// 使用调试功能
try
{
trans.call(SOAP_ACTION, envelope);
System.out.println(
"Call Successful!"
);
}
catch
(IOException e) {
System.out.println(
"IOException"
);
e.printStackTrace();
}
catch
(XmlPullParserException e) {
System.out.println(
"XmlPullParserException"
);
e.printStackTrace();
}
SoapObject result = (SoapObject) envelope.bodyIn;
return
result;
}
}
|
测试程序
1
2
3
4
5
6
7
8
9
10
11
12
|
package
junit.soap.wcf;
import
org.ksoap2.serialization.SoapObject;
public
class
HelloWcfTest {
public
static
void
main(String[] args) {
HelloService service =
new
HelloService(
"Master HaKu"
);
SoapObject result = service.LoadResult();
System.out.println(
"WCF返回的数据是:"
+ result.getProperty(
0
));
}
}
|
通过测试成功
运行结果:
Hello Master HaKu
6. Android客户端测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package
david.android.wcf;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
import
org.ksoap2.serialization.SoapObject;
public
class
AndroidWcfDemoActivity
extends
Activity {
private
Button mButton1;
private
TextView text;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
text = (TextView)
this
.findViewById(R.id.show);
mButton1.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View v) {
HelloService service =
new
HelloService(
"Master HaKu"
);
SoapObject result = service.LoadResult();
text.setText(
"WCF返回的数据是:"
+ result.getProperty(
0
));
}
});
}
}
<br>
|
7. 最后运行结果
安卓调用Webservice和Java稍有不一样,利用的是ksoap2这个jar包。以前这个jar包是发布在googlecode上面的目前项目已经移动到了github.io,我这里贴上的github官方网站,我也不知道这个github.io和github.com是否是一回事。咱们能够在如下页面看到项目的总览:http://simpligility.github.io/ksoap2-android/index.html。
1.下载ksoap2jar包
ksoap2项目的源码在这里,有兴趣的能够弄下来研究哦:
2.在Android Studio中进行配置
这一步简单,先放到lib文件夹下,而后再lib上点击右键,选择ADD AS LIB就能够了哦
3.利用网上的服务,自动生成ksoap2可用的webservice的客户端代理类
打开http://www.wsdl2code.com/pages/Home.aspx页面,在页面的右边填入你的webService的访问地址,而后选择生成的方式,我选的是Android Using kSoap2.若是你的webservice尚未发布,也能够直接上传其wsdl文件。
点击submit,此时要求登陆,若是没有帐号就注册一个,而后登录,稍等一会这个工具就会为咱们自动生成Webservice客户端代理类的代码了,点击下载
固然,自动生成的没有与jar运行环境啊什么的,可能使用的时候有些问题,至少包命名就得改为你本身的,因此,咱们再简单的修改一下这些代码就能够直接使用了,省去了咱们手动写客户端代理类的麻烦,是否是很方便啊。