Andoird + WCF Development Instance

Source: Internet
Author: User

Instance development architecture: Android calls the WCF Service (note that it is a service, not a restful style, but a restful style call later ).

Before getting started, first a ksoap2-android, which is the tool for calling the WCF Service. I downloaded it several times on the official website and found that it was not complete. So you can download http://download.csdn.net/detail/leesmn/5162649.

For the WCF section (vs2012 is used ):

1. Create the WCF class library odbservicelib. Define iservice and data conventions

Namespace odbservicelib
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "iservice1" in the Code and configuration file at the same time ".
[Servicecontract]
Public interface iservice
{
[Operationcontract]
Int postdaignosejson (string jsonstr );

[Operationcontract]
String postconsultjson (string jsonstr );

[Operationcontract]
Consult getconsult (string IMEI );
// Todo: add your service operation here
}

// Use the data conventions described in the following example to add a composite type to a service operation.
// Add the XSD file to the project. After a project is generated, you can directly use the data type defined in the namespace "odbservicelib. contracttype.
[Datacontract]
Public class Diagnose
{
[Datamember]
Public String IMEI {Get; set ;}
[Datamember]
Public double longpolling {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 longpolling {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. Implement Service. In this section of the Code, we used the Adobe .net entity framwork provided by vs as the orm operation database.

Namespace odbservicelib
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "service1" in the Code and configuration file at the same time ".
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 ("longpolling double ,");
Strb. append ("latitude double ,");
Strb. append ("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 ("(longpolling, latitude, datetime, type, content) values (");
Strb. append (D. longpolling );
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 ("longpolling double ,");
Strb. append ("latitude double ,");
Strb. append ("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 ("(longpolling, latitude, datetime, type, text, img1) values (");
Strb. append (C. longpolling );
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. Create a web-> WCF Service. Delete the default iservice and service. Add the odbservicelib class library to this project. Edit service. SVC tag

<% @ Servicehost Language = "C #" DEBUG = "true" service = "odbservicelib. Service" %>

By now, the WCF end has finished running to see if it can enable service. SVC.

Android:

1. Make sure that the ksoap2-android has been loaded into the project

Private Static final string namespace = "http://tempuri.org /";
Private Static final string url = "http: // 192.168.1.108: 8082/service. SVC ";

Private Static string method_name = "";
Private Static string soap_action = "";

Public static string call (string methodname, string jsonstr ){
String restr = "";
Method_name = methodname;
Soap_action = "http://tempuri.org/IService/" + methodname;

Try {

Soapobject request = new soapobject (namespace, method_name );
Request. addproperty ("jsonstr", jsonstr );
Soapserializationenvelope envelope = new soapserializationenvelope (
Soapenvelope. ver11 );
Envelope. DOTNET = true;
Envelope. setoutputsoapobject (request );

Httptransportse androidhttptransport = new httptransportse (URL );
Androidhttptransport. Call (soap_action, envelope );
Soapprimitive result = (soapprimitive) envelope. getresponse ();

// To get the data
Restr = result. tostring ();
// 0 is the first object of data

// Sb. append (resultdata + "\ n ");
} Catch (exception e ){
Restr = "error:" + E. getmessage ();
}

Return restr;
}

 

Private void postdaignosejson ()

{

Jsonobject object = new jsonobject ();
Try {
Object. Put ("IMEI", phoneimei );
Object. Put ("longpolling", X );
Object. Put ("latitude", y );
Object. Put ("datetime", SDF. Format (new date ()));
Object. Put ("type", type );
Object. Put ("content", diagdata );
} Catch (jsonexception e ){
E. printstacktrace ();
}
Call ("postdaignosejson", object. tostring ());

}

Private void postconsultjson (){

Imgstr = new string (base64.encode (imgbuf ));

Jsonobject object = new jsonobject ();
Try {
Object. Put ("IMEI", phoneimei );
Object. Put ("longpolling", X );
Object. Put ("latitude", y );
Object. Put ("datetime", SDF. Format (new date ()));
Object. Put ("type", 1 );
Object. Put ("text", consdata );
Object. Put ("imgbase64", imgstr );
} Catch (jsonexception e ){
E. printstacktrace ();
}

Call ("postconsultjson", object. tostring ());

}

Note: It is a bit difficult to upload images. You cannot convert byte [] to JSON. Therefore, encode base64.encode as string first.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.