Export to GitHub

android-ws-client - androidWSClientHowToV12.wiki


This is Step by Step guideline how to use this source codes/Projects.

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 one.

I am happy to have finished that 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 have to follow the following steps :

  1. Get the "androidWSClientGenDist-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).

  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", 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-feature android:name="android.hardware.location.network" />

```

  1. To consume any service you just need three simple line/step.
    • 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 sychronously

``` 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 ( );

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 );

``` Here we get the response.

  1. 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 {

@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.

  1. You can get the full project - ""AndroidWSSampleApp-1.2.0.zip" from the download tab.

  2. Acknowledgement :

So, that's all. Hope, In android web service development our life will be a little bit easier if you used this !!! Let's try it !!!!