In recent work, the project needs to develop an android client that calls the WCF Service to obtain data. I have never done Web Service Development on Android before. I checked it temporarily and sorted out a complete example.
Download test code
1. Create a WCF Service Project:
2. Add a simple method and output a string:
Note:UritemplateMultiple parameters. Bodystyle is specified to allow multiple parameters. The data format uses mobile terminal-friendly JSON format, and XML data redundancy is too high.
3. modify web. config
Use webhttpbinding for restful Web Services, and use basichttpbinding for soap Web Services.
4. After compilation is successful, use vs to publish it to IIS:
Create a website in IIS:
And then published to the website:
After the release is successful, check the WCF Service in the browser to confirm that the release is successful:
5. Start Android client development and testProgramCreate an android project in Eclipse:
6. Place a button and call the WCF Service when you click the button:
7. If the call is successful, record a log:
PackageCom. Brooks. wcfdemo;
ImportJava. Io. ioexception;
ImportJava. Io. inputstreamreader;
ImportJava. Io. unsupportedencodingexception;
ImportOrg. Apache. http. httpentity;
ImportOrg. Apache. http. httpresponse;
Import
Org. Apache. http. Client. Methods. httpget;
ImportOrg. Apache. http. impl. Client. defaulthttpclient;
ImportOrg. Apache. http. Protocol. HTTP;
ImportAndroid. App. activity;
ImportAndroid. OS. Bundle;
ImportAndroid. util. log;
ImportAndroid. View. Menu;
ImportAndroid. View. view;
Public
ClassMainactivityExtendsActivity {
@ Override
Public
VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (R. layout.Activity_main);
}
@ Override
Public
BooleanOncreateoptionsmenu (menu ){
Getmenuinflater (). Inflate (R. menu.Activity_main, Menu );
Return
True;
}
Public
VoidWcftestlinstener (view)
{
NewThread (Wcftest). Start ();
}
RunnableWcftest=NewRunnable (){
Public
VoidRun (){
//TodoAuto-generated method stub
Callwcf ();
}
};
Private
VoidCallwcf (){
Try{
// Send GET request to <service>/getplates
HttpgetRequest =New
Httpget("Http: // 192.168.0.100: 90/androidservice. svc/fntest/WCF");
Request. setheader ("Accept","Application/JSON");
Request. setheader ("Content-Type","Application/JSON");
Defaulthttpclient httpclient =NewDefaulthttpclient ();
Httpresponse response = httpclient.exe cute (request );
Httpentity responseentity = response. getentity ();
Log.D("WCF", Retrieveinputstream (responseentity ));
}Catch(Exception e ){
E. printstacktrace ();
}
}
ProtectedString retrieveinputstream (httpentity ){
IntLength = (Int) Httpentity. getcontentlength ();
If(Length <0)
Length = 10000;
Stringbuffer =NewStringbuffer (length );
Try{
Inputstreamreader =NewInputstreamreader (
Httpentity. getcontent (), HTTP.Utf_8);
CharBuffer [] =New
Char[Length];
IntCount;
While(COUNT = inputstreamreader. Read (buffer, 0, length-1)> 0 ){
Stringbuffer. append (buffer, 0, count );
}
}Catch(Unsupportedencodingexception e ){
}Catch(Illegalstateexception e ){
}Catch(Ioexception e ){
}
ReturnStringbuffer. tostring ();
}
}
Because Android later versions impose restrictions on operations on the main thread, asynchronous calls are required.
First, use the simulator to test:
After WiFi is enabled on the mobile phone, you can test it on the real machine: