WCF For Android.docx
- 文档编号:10856523
- 上传时间:2023-05-28
- 格式:DOCX
- 页数:12
- 大小:247.70KB
WCF For Android.docx
《WCF For Android.docx》由会员分享,可在线阅读,更多相关《WCF For Android.docx(12页珍藏版)》请在冰点文库上搜索。
WCFForAndroid
Android调用RESTfulWCF服务
WCF端
接口
[Description("REST服务测试")]
[ServiceContract]
publicinterfaceIAccountRestService:
IRestServiceContract
{
[WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json)]
List
[WebInvoke(Method="POST")]
List
///
/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
{
staticList
{
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;
}
}
publicList
{
returnAccountList;
}
publicList
{
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"> 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; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- WCF For Android