kSOAP2 – Based Web Service Client
If you are trying to get web services working on your Android device you have a few options. First, there is kSOAP. What is it? Well, it is a Java-based library for building SOAP clients. It was designed for Java clients with limited resources (Applets, Android Devices, and embedded solutions). But why do we need kSOAP, you ask. After all, there are rich libraries and tools in the existing JDKs for creating clients. True that. But SOAP communications require overhead that may be too much for mobile devices. (That is the thinking. I do not buy it. My XOOM has a ton of RAM and dual core NVidia chips. And the phones that are coming out are getting to be pretty well equipped with computing power.) But I do not recommend using kSOAP unless being mired in APIs is your thing. The whole point of web services was to add a layer of abstraction over the remote procedure call. In a typical kSOAP project there are numerous (relative) calls to low level APIs. For example, a simple call to the aforementioned HelloWorld looks like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
| package com.bif.client; import java.io.IOException; import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class KSOAP2Client extends Activity { private static final String stringMethodName = "getGreeting" ; TextView textView = null ; /** * Called when the activity is first created. */ public void onCreate(Bundle bundleSavedInstanceState) { super .onCreate(bundleSavedInstanceState); this .setContentView(R.layout.main); this .textView = (TextView)findViewById(R.id.textView); this .textView.setText( "Testing" ); AsyncTaskGreetingServiceCaller asyncTaskGreetingServiceCaller = new AsyncTaskGreetingServiceCaller(); asyncTaskGreetingServiceCaller.execute(); } private class AsyncTaskGreetingServiceCaller extends AsyncTask<Void, Void, Void> { protected Void doInBackground(Void...voids) { SoapObject soapObject = new SoapObject(KSOAP2Client.stringNamespace, KSOAP2Client.stringMethodName); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); soapSerializationEnvelope.setOutputSoapObject(soapObject); try { HttpTransportSE httpTransportSE = new HttpTransportSE(KSOAP2Client.stringURL); httpTransportSE.debug = true ; httpTransportSE.call(KSOAP2Client.stringNamespace + KSOAP2Client.stringMethodName, soapSerializationEnvelope); Object objectResult = (Object)soapSerializationEnvelope.getResponse(); textView.setText(objectResult.toString()); return null ; } catch (Exception exception) { exception.printStackTrace(); } return null ; } } } |
When this code runs on a tablet you will get a screen similar to the following
No comments:
Post a Comment