This is the second tutorial of Nano Tutorial series, in the first tutorial, I showed how to use Nano with a service called Number Conversion from data access worldwide. Number conversion is a simple service, its request/response structures are quite simple. Today I will show you how to use Pico with a more complex service called Barcode Generator from webserviceX.NET, barcode generator service has a more complex request/response structures, it will return encoded binary image data in the response.
By the way, since the wsdl driven development process in both tutorials are quite similar, I won’t repeat too much details in this tutorial, I suppose you have already read tutorial one and basically understand the wsdl driven development process supported by Nano.
During the code generation, you will get a few warnings say some ports in the wsdl will be ignored, just ignore these warnings, We won’t use those ports in this tutorial, it’s ok as long as the standard SOAP port is generated correctly.
By default, the proxy code will be generated in the sub-folder corresponding to the target namespace of the wsdl, you can use -p option to override the package name if needed.
There is a generated sub-folder called client, the SOAP and XML proxy interface will be generated in this folder.
Step 2 - Create New Android Project, Add Nano Library and Generated Proxy into Your Project
Create a simple Android project in eclipse(or other IDE you prefer), then:
packagecom.leansoft.nano.sample;importcom.leansoft.nano.soap11.Fault;importcom.leansoft.nano.ws.SOAPServiceCallback;importnet.webservicex.BarCodeData;importnet.webservicex.BarcodeOption;importnet.webservicex.BarcodeType;importnet.webservicex.CheckSumMethod;importnet.webservicex.GenerateBarCode;importnet.webservicex.GenerateBarCodeResponse;importnet.webservicex.ImageFormats;importnet.webservicex.ShowTextPosition;importnet.webservicex.client.BarCodeSoap_SOAPClient;importnet.webservicex.service.BarCodeServiceClient;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.ImageView;importandroid.widget.Toast;importandroid.app.Activity;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;publicclassMainActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButtonencodeButton=(Button)this.findViewById(R.id.encodeButton);encodeButton.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){// get shared clientBarCodeSoap_SOAPClientclient=BarCodeServiceClient.getSharedClient();client.setDebug(true);// enable soap message logging// build requestBarCodeDatabarCodeData=newBarCodeData();barCodeData.height=125;barCodeData.width=225;barCodeData.angle=0;barCodeData.ratio=5;barCodeData.module=0;barCodeData.left=25;barCodeData.top=0;barCodeData.checkSum=false;barCodeData.fontName="Arial";barCodeData.barColor="Black";barCodeData.bgColor="White";barCodeData.fontSize=10.0f;barCodeData.barcodeOption=BarcodeOption.BOTH;barCodeData.barcodeType=BarcodeType.CODE_2_5_INTERLEAVED;barCodeData.checkSumMethod=CheckSumMethod.NONE;barCodeData.showTextPosition=ShowTextPosition.BOTTOM_CENTER;barCodeData.barCodeImageFormat=ImageFormats.PNG;GenerateBarCoderequest=newGenerateBarCode();request.barCodeParam=barCodeData;request.barCodeText=((EditText)findViewById(R.id.barCodeText)).getText().toString();// make API call with registered callbacksclient.generateBarCode(request,newSOAPServiceCallback<GenerateBarCodeResponse>(){@OverridepublicvoidonSuccess(GenerateBarCodeResponseresponseObject){// successImageViewbarCodeImage=(ImageView)findViewById(R.id.barCodeImage);byte[]imageData=responseObject.generateBarCodeResult;Bitmapbitmap=BitmapFactory.decodeByteArray(imageData,0,imageData.length);barCodeImage.setImageBitmap(bitmap);}@OverridepublicvoidonFailure(Throwableerror,StringerrorMessage){// http or parsing errorToast.makeText(MainActivity.this,errorMessage,Toast.LENGTH_LONG).show();}@OverridepublicvoidonSOAPFault(ObjectsoapFault){// soap faultFaultfault=(Fault)soapFault;Toast.makeText(MainActivity.this,fault.faultstring,Toast.LENGTH_LONG).show();}});}});}}
The service invocation logic is quite self explanatory, you get shared SOAP barcode proxy client first, then build encoding request, make API call with the request and registered callbacks, if API call is successful, you just display the barcode image on UI, otherwise, you display error message accordingly using Android Toast UI component.