WCF For Android.docx

上传人:b****3 文档编号:10856523 上传时间:2023-05-28 格式:DOCX 页数:12 大小:247.70KB
下载 相关 举报
WCF For Android.docx_第1页
第1页 / 共12页
WCF For Android.docx_第2页
第2页 / 共12页
WCF For Android.docx_第3页
第3页 / 共12页
WCF For Android.docx_第4页
第4页 / 共12页
WCF For Android.docx_第5页
第5页 / 共12页
WCF For Android.docx_第6页
第6页 / 共12页
WCF For Android.docx_第7页
第7页 / 共12页
WCF For Android.docx_第8页
第8页 / 共12页
WCF For Android.docx_第9页
第9页 / 共12页
WCF For Android.docx_第10页
第10页 / 共12页
WCF For Android.docx_第11页
第11页 / 共12页
WCF For Android.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

WCF For Android.docx

《WCF For Android.docx》由会员分享,可在线阅读,更多相关《WCF For Android.docx(12页珍藏版)》请在冰点文库上搜索。

WCF For Android.docx

WCFForAndroid

Android调用RESTfulWCF服务

WCF端

接口

[Description("REST服务测试")]

[ServiceContract]

publicinterfaceIAccountRestService:

IRestServiceContract

{

[WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json)]

ListGetAccountDataByGet();

[WebInvoke(Method="POST")]

ListGetAccountDataByPost();

///调用方式:

/SendMessageByGet1?

message=aaa&value=3

[WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json)]

stringSendMessageByGet1(stringmessage,intvalue);

///调用方式:

/SendMessageByGet/aaa/3

[WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json,UriTemplate="/SendMessageByGet2/{message}/{value}")]

stringSendMessageByGet2(stringmessage,stringvalue);

///调用方式:

{"message":

"aa","value":

3}。

另外不要忘了在HTTP头中加入“content-type:

text/jsoncontent-length:

26”。

BodyStyle特性:

方法参数若是多个,必须对其进行Json包装

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]

stringSendMessageByPost1(stringmessage,intvalue);

}

publicclassAccountService:

IAccountRestService

{

staticListAccountList

{

get

{

varlist=newList

{

newAccount

{

Name="BillGates",

Address="YouYiEastRoad",

Age=56,

Birthday=DateTime.Now

},

newAccount

{

Name="StevePaulJobs",

Address="YouYiWestRoad",

Age=57,

Birthday=DateTime.Now

},

newAccount

{

Name="JohnD.Rockefeller",

Address="YouYiNorthRoad",

Age=65,

Birthday=DateTime.Now

}

};

returnlist;

}

}

publicListGetAccountDataByGet()

{

returnAccountList;

}

publicListGetAccountDataByPost()

{

returnAccountList;

}

publicstringSendMessageByGet1(stringmessage,intvalue)

{

returnDateTime.Now+"GetMessage:

"+message+value;

}

publicstringSendMessageByGet2(stringmessage,stringvalue)

{

returnDateTime.Now+"GetMessage:

"+message+value;

}

publicstringSendMessageByPost1(stringmessage,intvalue)

{

returnDateTime.Now+"PostMessage:

"+message+value;

}

#regionImplementationofIDisposable

publicvoidDispose()

{

}

#endregion

}

托管配置

Bindings.Add(newWebHttpBinding

{

CrossDomainScriptAccessEnabled=true

}

newList

{

newWebHttpBehavior(),

});

ANDROID端

界面

android="

xmlns:

tools="

android:

layout_width="fill_parent"

android:

layout_height="fill_parent">

android:

id="@+id/tableLayout1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content">

android:

id="@+id/btnGetAccountDataByGet"

android:

text="GetAccountDataByGet"/>

android:

id="@+id/btnGetAccountDataByPost"

android:

text="GetAccountDataByPost"/>

android:

id="@+id/btnSendMessageByGet1"

android:

text="SendMessageByGet1"/>

android:

id="@+id/btnSendMessageByGet2"

android:

text="SendMessageByGet2"/>

android:

id="@+id/btnSendMessageByPost1"

android:

text="SendMessageByPost1"/>

packagecom.example.testwcf;

importorg.apache.http.HttpResponse;

importorg.apache.http.client.HttpClient;

importorg.apache.http.client.methods.HttpGet;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.entity.StringEntity;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.protocol.HTTP;

importorg.apache.http.util.EntityUtils;

importorg.json.JSONObject;

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.Toast;

publicclassMainActivityextendsActivity{

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

{

Buttonbutton=(Button)super

.findViewById(R.id.btnGetAccountDataByGet);

button.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

try{

HttpClientclient=newDefaultHttpClient();

HttpGetrequest=newHttpGet(

"http:

//10.168.1.102:

9999/IAccountRestService/GetAccountDataByGet");

HttpResponseresponse=client.execute(request);

Toast.makeText(MainActivity.this,

EntityUtils.toString(response.getEntity()),

Toast.LENGTH_SHORT).show();

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

{

Buttonbutton=(Button)super

.findViewById(R.id.btnGetAccountDataByPost);

button.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

try{

HttpClientclient=newDefaultHttpClient();

HttpPostrequest=newHttpPost(

"http:

//10.168.1.102:

9999/IAccountRestService/GetAccountDataByPost");

HttpResponseresponse=client.execute(request);

Toast.makeText(MainActivity.this,

EntityUtils.toString(response.getEntity()),

Toast.LENGTH_SHORT).show();

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

{

Buttonbutton=(Button)super

.findViewById(R.id.btnSendMessageByGet1);

button.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

try{

HttpClientclient=newDefaultHttpClient();

HttpGetrequest=newHttpGet(

"http:

//10.168.1.102:

9999/IAccountRestService/SendMessageByGet1?

message=aaa&value=3");

HttpResponseresponse=client.execute(request);

Toast.makeText(MainActivity.this,

EntityUtils.toString(response.getEntity()),

Toast.LENGTH_SHORT).show();

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

{

Buttonbutton=(Button)super

.findViewById(R.id.btnSendMessageByGet2);

button.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

try{

HttpClientclient=newDefaultHttpClient();

HttpGetrequest=newHttpGet(

"http:

//10.168.1.102:

9999/IAccountRestService/SendMessageByGet2/bbb/4");

HttpResponseresponse=client.execute(request);

Toast.makeText(MainActivity.this,

EntityUtils.toString(response.getEntity()),

Toast.LENGTH_SHORT).show();

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

{

Buttonbutton=(Button)super

.findViewById(R.id.btnSendMessageByPost1);

button.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

try{

HttpClientclient=newDefaultHttpClient();

HttpPostrequest=newHttpPost(

"http:

//10.168.1.102:

9999/IAccountRestService/SendMessageByPost1");

JSONObjectp=newJSONObject();

p.put("value",4);

p.put("message","ccc");

request.setEntity(newStringEntity(p.toString()));

request.setHeader(HTTP.CONTENT_TYPE,"text/json");

HttpResponseresponse=client.execute(request);

Stringre=EntityUtils.toString(response.getEntity());

Toast.makeText(MainActivity.this,re,

Toast.LENGTH_SHORT).show();

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

}

@Override

publicbooleanonCreateOptionsMenu(Menumenu){

getMenuInflater().inflate(R.menu.activity_main,menu);

returntrue;

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 其它课程

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2