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運行環境啊什麼的,可能使用的時候有些問題,至少包命名就得改爲你本身的,因此,咱們再簡單的修改一下這些代碼就能夠直接使用了,省去了咱們手動寫客戶端代理類的麻煩,是否是很方便啊。