實例開發架構:android調用wcf service服務(注意是service服務,不是restful風格,後面再講restful風格的調用)。
動手之前,先一個ksoap2-android,它是我們調用wcf服務的工具。在官網上下載過好幾次都不能用,後來發現原來下載的不是完整的。所以大家可以到這下載http://download.csdn.net/detail/leesmn/5162649。
wcf部分(使用的是VS2012):
1、新建wcf類庫odbServiceLib。定義IService,以及定義數據約定
namespace odbServiceLib
{
// 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService
{
[OperationContract]
int PostDaignoseJson(string jsonStr);
[OperationContract]
string PostConsultJson(string jsonStr);
[OperationContract]
Consult GetConsult(string imei);
// TODO: 在此添加您的服務操作
}
// 使用下面示例中說明的數據約定將復合類型添加到服務操作。
// 可以將 XSD 文件添加到項目中。在生成項目後,可以通過命名空間“odbServiceLib.ContractType”直接使用其中定義的數據類型。
[DataContract]
public class Diagnose
{
[DataMember]
public string imei { get; set; }
[DataMember]
public double longitude { get; set; }
[DataMember]
public double latitude { get; set; }
[DataMember]
public string datetime { get; set; }
[DataMember]
public int type { get; set; }
[DataMember]
public string content { get; set; }
}
[DataContract]
public class Consult
{
[DataMember]
public string imei { get; set; }
[DataMember]
public double longitude { get; set; }
[DataMember]
public double latitude { get; set; }
[DataMember]
public string datetime { get; set; }
[DataMember]
public int type { get; set; }
[DataMember]
public string text { get; set; }
[DataMember]
public string imgstr { get; set; }
[DataMember]
public byte[] img1 { get; set; }
}
}
2、實現Service。在該段代碼中本人用了vs自帶的ado.net entity framwork作為orm操作數據庫
namespace odbServiceLib
{
// 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的類名“Service1”。
public class Service : IService
{
public int PostDaignoseJson(string jsonStr)
{
Diagnose d;
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
{
DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(Diagnose));
d = (Diagnose)serializer1.ReadObject(ms);
}
//System.Data.Common.DbTransaction tran = null;
int i = 0;
try
{
using (carobdEntities ctx = new carobdEntities())
{
ctx.Database.Connection.Open();
//tran = ctx.Database.Connection.BeginTransaction();
StringBuilder strb = new StringBuilder();
strb.Append("create table if not EXISTS diag");
strb.Append(d.imei);
strb.Append("(id int not null auto_increment,");
strb.Append("longitude double,");
strb.Append("latitude double,");
strb.Append("datetime datetime,");
strb.Append("type int,");
strb.Append("content varchar(500),");
strb.Append("PRIMARY KEY (id) )");
if (ctx.Database.ExecuteSqlCommand(strb.ToString()) > 0)
ctx.SaveChanges();
strb.Clear();
strb.Append("insert into diag");
strb.Append(d.imei);
strb.Append("(longitude,latitude,datetime,type,content)values(");
strb.Append(d.longitude);
strb.Append(",");
strb.Append(d.latitude);
strb.Append(",'");
strb.Append(d.datetime);
strb.Append("',");
strb.Append(d.type);
strb.Append(",'");
strb.Append(d.content);
strb.Append("')");
if (ctx.Database.ExecuteSqlCommand(strb.ToString()) > 0)
i = ctx.SaveChanges();
//tran.Commit();
}
}
catch { i = -1; }
return i;
}
public string PostConsultJson(string jsonStr)
{
Consult c;
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
{
DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(Consult));
c = (Consult)serializer1.ReadObject(ms);
}
c.img1 = Convert.FromBase64String(c.imgstr);
//System.Data.Common.DbTransaction tran = null;
int i = 0;
try
{
using (carobdEntities ctx = new carobdEntities())
{
ctx.Database.Connection.Open();
//tran = ctx.Database.Connection.BeginTransaction();
StringBuilder strb = new StringBuilder();
strb.Append("create table if not EXISTS cons");
strb.Append(c.imei);
strb.Append("(id int not null auto_increment,");
strb.Append("longitude double,");
strb.Append("latitude double,");
strb.Append("datetime datetime,");
strb.Append("type int,");
strb.Append("text varchar(500),");
strb.Append("img1 longblob,");
strb.Append("PRIMARY KEY (id) )");
if (ctx.Database.ExecuteSqlCommand(strb.ToString()) > 0)
ctx.SaveChanges();
strb.Clear();
strb.Append("insert into cons");
strb.Append(c.imei);
strb.Append("(longitude,latitude,datetime,type,text,img1)values(");
strb.Append(c.longitude);
strb.Append(",");
strb.Append(c.latitude);
strb.Append(",'");
strb.Append(c.datetime);
strb.Append("',");
strb.Append(c.type);
strb.Append(",'");
strb.Append(c.text);
strb.Append("',");
strb.Append("{0}");
strb.Append(")");
if (ctx.Database.ExecuteSqlCommand(strb.ToString(), c.img1) > 0)
i = ctx.SaveChanges();
//tran.Commit();
}
}
catch (Exception e) { i = -1; return e.Message; }
return i.ToString();
}
public Consult GetConsult(string imei)
{
Consult c = null;
try
{
using (carobdEntities ctx = new carobdEntities())
{
ctx.Database.Connection.Open();
//tran = ctx.Database.Connection.BeginTransaction();
StringBuilder strb = new StringBuilder();
strb.Append("select * from cons");
strb.Append(imei);
strb.Append(" order by id DESC limit 1");
c = ctx.Database.SqlQuery<Consult>(strb.ToString()).FirstOrDefault();
//tran.Commit();
}
}
catch (Exception e) { }
return c;
}
}
}
3、新建web-〉wcf服務。將默認生成的IService以及Service刪除掉。將odbServiceLib 類庫添加到本項目中。編輯Service.svc的標記
<%@ ServiceHost Language="C#" Debug="true" Service="odbServiceLib.Service" %>
到此wcf端就大功告成了運行一下看看能不能打開Service.svc