Search This Blog

Loading...

Monday, January 16, 2012

How to call the asp .Net web service in android

First of all make sure that your web service is in the proper url and get that url with you.

Just like for example i use the web service of "http://www.webservicex.net/ConvertWeight.asmx" for converting the weights from one unit to another.

Now insert the external jar file of ksoap2 which can be downloaded from the following links

To make use of this library in a non-Maven project, follow the instructions in the Android Developer's Guide on how to Add an External Library to your project.

You will need to add a ksoap2-android and all required transitive dependencies to the build path. Luckily the Maven build of the project produces a nice bundle of all these jars in one big file.

The different version of these files are available at

http://code.google.com/p/ksoap2-android/source/browse/#svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/

To download a file from there, right click on "View raw file" and select "Save Link as" (this label differs for different browsers) and you will get the full jar downloaded.

The latest release artifact would be available at

http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.6.0/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar

with a direct download url of
http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.6.0/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar

Some users seem to experience download problems with IE. Just try a decent browser or download with a command line tool like wget
wget http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.6.0/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar

Now make the java file and insert the following in that code :


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class WebserActivity extends Activity {
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";

/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
        
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

String weight = "3700";
String fromUnit = "Grams";
String toUnit = "Kilograms";

PropertyInfo weightProp =new PropertyInfo();
weightProp.setName("Weight");
weightProp.setValue(weight);
weightProp.setType(double.class);
request.addProperty(weightProp);

PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("FromUnit");
fromProp.setValue(fromUnit);
fromProp.setType(String.class);
request.addProperty(fromProp);

PropertyInfo toProp =new PropertyInfo();
toProp.setName("ToUnit");
toProp.setValue(toUnit);
toProp.setType(String.class);
request.addProperty(toProp);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {

    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
    Log.i("myApp", response.toString());
    
    TextView tv = new TextView(this);
     tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
     setContentView(tv);

        } catch (Exception e) {
                       e.printStackTrace();
        }

    }
}




Now make an xml file named main.xml and insert the following code into it :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="vertical" >

<TextView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/hello" />
</LinearLayout>


And finally manifest file should be looks like this : 



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sencide"
      android:versionCode="1"
      android:versionName="1.0">

    <application android:label="@string/app_name">
        <activity android:name=".WebserActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.