Tag Archives: Android

Access .Net webservices which return complex types

I thought it was pretty simple for a beginner like me to parse the dataset returned from .Net webservices. I tried all the options, tried to map like
envelope.addMapping(“Namespace”, “class or tag”, classname);
but this one works fine with the complex types not with the datasets. Or maybe I might have missed something, I was not able to parse data.

For simple types we can use this

private SoapObject table = null;       // Its the table of a DataSet
private SoapObject client = null;       // Its the client pettition to the web service
private SoapObject tableRow = null;      // Gets the information of a row from the table DataSet
private SoapObject responseBody = null;     // Its the whole response of the WebService
private HttpTransport transport = null;     // Its whta is used to make the call to the WebService
private SoapSerializationEnvelope sse = null;
sse = new SoapSerializationEnvelope(SoapEnvelope.VER11); // SOAP version is indicated here. Version 11 works nice
sse.addMapping(<webService NameSpace>, <name of the Class using it>, this.getClass());
sse.dotNet = true; // We stablish we sill be working with .NET webservices
transport = new HttpTransport(<url of the webservices>);
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); transporte.debug = debug; // Set it to either use transport.requestdump or reponsedump to see the xml of the webservice request or response
// Here we set who are we gonna call, give the method arguments and do the call
client = new SoapObject(<webServiceNameSpace>, <webService Method Name we want to reuqest>);
// You can add as many properties as many as you need. I'd say that you set your webservices method to recieve only strings and do the convertions on you webservice. Its quite hard work with primitive values
client.addProperty(<agument name as String>, <argument Value>);
sse.setOutputSoapObject(client);
sse.bodyOut = client;
transporte.call(<webService direct url>, sse);
// We'll get the whole XML reponse with all the information
responseBody = (SoapObject) sse.getResponse();
// Now, the reponse works as a Vector. On position 0 we have the information of the Reponse while in position 1 we get the info we requested. So we get rid of the information reponse and lets grab the info we requested
responseBody = (SoapObject) responseBody.getProperty(1);
// And now, we have the n tables the WebService returned to us with the information, but still we can't access the info, so we'll get one of the tables separated. We'll use a very different variable for not to lose the whole info the webservice sended us. Once again, the tables are contained on a Vector. Lets grab the first one
table = (SoapObject) responseBody.getProperty(0);
// Now we have the table, but still can't use the info, soooo, lets grab a row individually
tableRow = (SoapObject) table.getProperty(0);

tableRow.getProperty(<columnName>).toString()

sse.getResponse()

It worked pretty fine when we have few records.

I parsed the responsedump xml to parse the response data into objects using SAXParser. I felt this one to be easy way.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XMLReader reader = parser.getXMLReader();
//Use the handler to parse the XML
ExampleHandler handler = new ExampleHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(new ByteArrayInputStream(transport.responsedump.getbytes())));
Object object = handler.parsedata();

The response can be captured from handler. You need to create a handler with the parsedata.

Hope this helps..

Consuming webservice from Android

I was trying to consume .Net webservices from Android application using ksoap2-for-android sdk, I was getting proper response when there are no parameters to pass. When I’m trying to pass parameters, I was not succeeded. I seached lots of forums, there are so many suggestions, after implementing them I was able to send parameters and get the response correctly.

This was my code:

private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String strURL = "http://w3schools.com/webservices/tempconvert.asmx";
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv =(TextView)findViewById(R.id.TextView01);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius","23.5");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.encodingStyle=SoapSerializationEnvelope.ENC;
envelope.setOutputSoapObject(request);
AndroidHttpTransport transport ;
try
{
transport = new AndroidHttpTransport(strURL);
transport.debug=true;
transport.setXmlVersionTag(/"<?xml version=/"1.0/" encoding=/"utf-8/"?/>");
transport.call(SOAP_ACTION, envelope);
SoapPrimitive resultString = (SoapPrimitive)envelope.getResponse();
tv.setText("Status:" + resultString.toString());
catch(Exception e){
e.printStackTrace();
}
}

To resolve the issue regarding Outbound parameters passed as null, the following suggestions were been provided:

1. If you are using IE set to Access data across domains to enabled
2. change addProperty(“Temperature”,”23.5″); to addProperty(“arg0″,”23.5”); even without implementing this suggestion also I was able to connect
3. Set the encoding style for the envelope.
4. Set the boolean dotnet if you are connecting .Net web service
5. There is also a workaround to use ksoap and java.net tools combined to connect to webservice with the entire soap body specified.
6. Set the Content-Type
7. And if you are connecting a webservice on your localmachine, then provide the ipaddress rather than localhost/webservicename like 198.10.129.2/webservicename
8. Sometimes UAC blocks the connection, try running eclipse in administrator mode.

These are the suggestions kept together any of them may work for you..

Hope this saves someone’s day

Development in Android

Android is a mobile phone operating system initially developed by Android Inc, later purchased by Google. Anyone can download the SDK (software development kit) and write applications for Android phones. One can download the sdk at http.developer.android.com/sdk/. The sdk will allow you to develop, test, profile and debug using the emulator and other included tools.

The Android SDK provides tools and APIs to begin developing

Google doesn’t screen developers or applications. Google doesn’t even take part of the profits.

Google formed a group of hardware, software, and telecommunication companies called the Open Handset Alliance with the goal of contributing to Android development.

To develop usind Android SDK, as per the Android guide, its much easier to develop apps using eclipse. The basic requirements to develop using android are, download latest version of Android SDK and install it, download latest version of JDK (only JRE is not enough) and latest version of eclipse IDE for Java developers. Once you installed ecplipse, install the Android Development Toolkit (ADT). For eclipse galileo, start eclipse go to Help -> install new software and
In the Available Software dialog, click Add….
In the Add Site dialog that appears, enter a name for the remote site (for example, “Android Plugin”) in the “Name” field.
In the “Location” field, enter this URL:https://dl-ssl.google.com/android/eclipse/
Note: If you have trouble acquiring the plugin, you can try using “http” in the URL, instead of “https” (https is preferred for security reasons).
Click OK.
Back in the Available Software view, you should now see “Developer Tools” added to the list. Select the checkbox next to Developer Tools, which will automatically select the nested tools Android DDMS and Android Development Tools. Click Next.
In the resulting Install Details dialog, the Android DDMS and Android Development Tools features are listed. Click Next to read and accept the license agreement and install any dependencies, then click Finish.
Restart Eclipse