Sunteți pe pagina 1din 7

Android Web Service Generator Developer Guide

@author : Asraful Haque Sohel


Email : asraf344@gmail.com Version : 1.2.0

[This document is a step by step Guide on how to Use the Android WS Client Generator in both Windows platform]

The "Jinouts Android Web Service client" is an effort to develop an open source Web service client generator in android platform. I have googled to have an "easy-to-use" web service client for the android platform. I didn't find an easy one so I decided to develop this one. I am happy to have finished this task. I would be happier if it comes to any use of any developers in any corner of the world. To use this "Android Web Service Client Generation tool", we can proceed with the the following steps: 1. Get the "android-ws-client-1.2.0.zip" file from the download link of this project ( "http://code.google.com/p/android-ws-client/downloads/list" ). 2. Unzip it and run the " runJinoutsAndroidWSClient.bat" batch file. 3. It will show you a UI where you can input the ".wsdl" file url and the location of the client stub. Choose the CXF Bin dir. You can download the cxf distribution from here http://www.apache.org/dyn/closer.cgi?path=/cxf/2.6.1/apache-cxf-2.6.1.zip extracting this zip you will get the distribution where there is a bin dir. Or you can also define the CXF_HOME as the environmental variable. Click the button Generate Stub. It will generate the java source code and some necessary library as well at your client stub directory.

4. Get the libraries (.jar) from the folder "andr-lib" and make sure that you put these libs to the classpath of your android project (say make a folder named "lib" in your project and put the jars there and add those library in the build path).
2

5. Get the Source Code from the client stub dir and put those in your project source dir. 6. Its done !!! Now, you are ready to consume your web service from the android project. 7. Let me give an example. I have had a test with my some local web service. Lets say, we have a simple Web Service named "LoginService". I have exposed this service using jax-ws and spring. 8. To get this web service get the project "JinoutsWebService.zip" from the download tab and run the test class "WebServiceRunner". Your service is exposed. To check this in your browser hit in this url "http://116.68.194.105:9010/LoginService?WSDL", you will see the wsdl if its successfully exposed. So, your web service is exposed now. 9. Now the time has come to consume this service using the client stub. Lets create the android project named " AndroidWSSampleApp-1.2.0.zip". From this project we like to consume the login service. Use step 1-6 to get the client stub for this service. 10. Create two text field "user name" and "password" and a button to login. In the button "onClick" lets put "callWebService".

Lets create a function named "callWebService" in the activity class to implement this, So that when a user click this button with the user name and password this method will be called. 11. As it will access the network give the application network access by the following configuration at the end of "AndroidManifest.xml" file :
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-feature android:name="android.hardware.location.network" />

12. To consume any service you just need three simple lines/steps : First instantiate the service, Secondly, get endpoint port to call the service. And Thirdly, Call the service just like method call.

For this example it will be like this: Before calling the Service set the thread policy to allow the web service call synchronously
StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork( ).detectAll ( ).penaltyLog().build()); // Here Just two lines to make the web service ready to call LoginService service = new LoginService ( ); Login login = service.getLoginPort ( );

1. Instantiate the service.

2. Get endpoint port to call Service.

EditText userEditText = (EditText)findViewById ( R.id.userIdText ); EditText passEditText = (EditText)findViewById ( R.id.passEditText ); String user = userEditText.getText ( ).toString ( ); String passwd = passEditText.getText ( ).toString ( ); // Here Call the Web service, The simplest way LoginServiceResponse resp = login.login ( user, passwd );

3. Call the service like a method call.

Here we get the response. 13. Lets consume the service asynchronously- Its the preferred way actually. For this, Lets define a "AsyncTask" to consume the service in background. So, the class will be like the following :

private class LoginServiceTask extends AsyncTask<Void, Void, LoginServiceResponse> {

@Override protected LoginServiceResponse doInBackground ( Void... params ) { // Here Just two lines to make the web service ready to call LoginService service = new LoginService ( ); Login login = service.getLoginPort ( );

EditText userEditText = (EditText)findViewById ( R.id.userIdText ); EditText passEditText = (EditText)findViewById ( R.id.passEditText ); String user = userEditText.getText ( ).toString ( ); String passwd = passEditText.getText ( ).toString ( ); // Here Call the Web service, The simplest way LoginServiceResponse resp = login.login ( user, passwd ); return resp; } @Override protected void onPostExecute(LoginServiceResponse resp) { TextView respTextView = (TextView)findViewById ( R.id.respTextView ); if ( resp != null ) { Log.i ( "AndroidWSAppActivity ", "Success Code From Resp !!!!!!: "+ resp.getSuccessCode ( ) ); Log.i ( "AndroidWSAppActivity ", "Error Code From Resp !!!!!!: "+ resp.getErrorCode ( ) ); respTextView.setText ( "SuccessCode: "+resp.getSuccessCode ( ) + " Message: "+resp.getSuccessMessage ( ) +" \nErrorCode: "+resp.getErrorCode ( ) + " Message: "+resp.getErrorMessage ( ) ); } else { respTextView.setText ( "Couldn't get Response" ); } } }

Look, here I have shown the response in another text View. Finally, to call the service async task :
LoginServiceTask loginTask = new LoginServiceTask ( ); loginTask.execute ( new Void[]{} );

The final ui should be like the following (with userId : asraf and pass: asraf) :
5

14. I have done with some simple request/response. It should work for complex request/response as well. You can get the full project - "AndroidWSSampleApp-1.2.0.zip" from the download tab. 15. Acknowledgement : a. I have used the "cxf distribution" (http://cxf.apache.org) to get the client stub (java code) from the wsdl. b. Get some utility classes from Soap UI source code (http://www.soapui.org/). c. Used some two classes from the Android soap (http://wiki.javaforum.hu/display/ANDROIDSOAP/Home). Definitely I have modify some of it. d. And definitely I have used some third party jars which are explicitly in the project. So, that's all. Hope, In android web service development out life will be a little bit easier if you used this !!! Let's try it !!!!

End of Document

@author : Asraful Haque Sohel


Email : asraf344@gmail.com

S-ar putea să vă placă și