Wednesday, November 10, 2010

How to add two values using Webservice in Android

I am running webservice successfully.
I am giving sample code will help you.

first create Dynamic Web project using eclipse.
and create one class : Converter.java
This class place under this package: org.web.example

package org.web.example

public class Converter {
public int sum(int i, int j) {
return (i + j);
}
}

now select Converter.java file and do right click and

New–> Other–> Web Services–> and select Web Service
There are two option
1. Web Service Type
2. Client Type
in first point you have to select slider as a Start

Service:
and in second point you have to select slider as a Test

Client
and click on Finish button.

You web Service will run automatically. Just check your

webservice will work properly or not.

Main Steps:
Now most important thing is you have to download ksoap2

jar file
download from here
http://www.4shared.com/file/uOhx5aoj/ksoap2-android-

assembly-24-jar.html

Now Create Android Project
and copy paste this code

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import

org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
private String METHOD_NAME = “sum”; // our

webservice method name
private String SOAP_ACTION =

“http://example.web.org/” + METHOD_NAME; // here your

package name in webservice but in reverse order + method

name
private String NAMESPACE =

“http://example.web.org”; // Here package name in

webservice with reverse order.
private static final String URL =

“http://192.168.1.2:8787/WebServiceDemo/services/Convert

er?wsdl”; // you must use ipaddress here, don’t use

Hostname or localhost

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

((TextView) findViewById

(R.id.txtsearch)).setText(“Hello”);
try {
SoapObject request = new

SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty(“i”, 5);
request.addProperty(“j”, 15);
SoapSerializationEnvelope

envelope = new SoapSerializationEnvelope

(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject

(request);

AndroidHttpTransport

androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call

(SOAP_ACTION,envelope);

Object result =

envelope.getResponse();
System.out.println(“Result : ” +

result.toString());
((TextView) findViewById

(R.id.txtsearch)).setText(result.toString());

} catch (Exception E) {
E.printStackTrace();
((TextView) findViewById

(R.id.txtsearch)).setText(“ERROR:”
+ E.getClass

().getName() + “: ” + E.getMessage());
}
}

And run it will give sum of two value


Thanks & Regards,

Prashant Adesara

1 comment:

  1. hey i found useful for me and is indeed working well.
    However i wanted some help regarding a similar thing

    Let's say I have a local my sql database on my pc , the one we get with wampserver and I want to make an app in android that will help me to insert , delete and update ( 3 buttons) my database records i.e first connect with database and then do the operations

    can u gimme me some suggestions on the same ?

    Thanks

    ReplyDelete