当前位置:操作系统 > 安卓/Android >>

Android调用RESTful WCF服务

WCF端
接口
[csharp]
[Description("REST服务测试")] 
[ServiceContract] 
public inte易做图ce IAccountRestService : IRestServiceContract 

    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    List<Account> GetAccountDataByGet(); 
 
    [WebInvoke(Method = "POST")] 
    List<Account> GetAccountDataByPost(); 
 
    /// <example>调用方式:/SendMessageByGet1?message=aaa&value=3</example> 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    string SendMessageByGet1(string message, int value); 
 
    /// <example>调用方式:/SendMessageByGet/aaa/3</example> 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SendMessageByGet2/{message}/{value}")] 
    string SendMessageByGet2(string message, string value); 
 
    /// <example>调用方式:{"message":"aa","value":3}。另外不要忘了在HTTP头中加入“content-type: text/json content-length: 26”。BodyStyle特性:方法参数若是多个,必须对其进行Json包装</example> 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    string SendMessageByPost1(string message, int value); 


实现
[csharp] 
public class AccountService : IAccountRestService 

    static List<Account> AccountList 
    { 
        get 
        { 
            var list = new List<Account> 
                           { 
                               new Account 
                                   { 
                                       Name = "Bill Gates", 
                                       Address = "YouYi East Road", 
                                       Age = 56, 
                                       Birthday = DateTime.Now 
                                   }, 
                               new Account 
                                   { 
                                       Name = "Steve Paul Jobs", 
                                       Address = "YouYi West Road", 
                                       Age = 57, 
                                       Birthday = DateTime.Now 
                                   }, 
                               new Account 
                                   { 
                                       Name = "John D. Rockefeller", 
                                       Address = "YouYi North Road", 
                                       Age = 65, 
                                       Birthday = DateTime.Now 
                                   } 
                           }; 
            return list; 
        } 
    } 
 
    public List&

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,