Question

I'm trying to invoke a add method of asp.net Web Service which takes two integers as parameters and return the sum of them. The web service is deployed and is running in local IIS server.

But while executing the apk in android emulator it threw following errors.

05-08 18:14:53.668: E/AndroidRuntime(662): FATAL EXCEPTION: Thread-81

05-08 18:14:53.668: E/AndroidRuntime(662): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject

05-08 18:14:53.668: E/AndroidRuntime(662): at com.example.adddemo.CallSoap.Add(CallSoap.java:23)

05-08 18:14:53.668: E/AndroidRuntime(662): at com.example.adddemo.Caller.run(Caller.java:14)

What am I missing here? I have imported ksoap2 library. Please let me know if I have missed anything that needs to be added extra while sending the request or is there any problem with my local IIS.

My complete code is as follows:

activity_main.xml has two edit text and a button. On button click a method in ActivityMain.java should be called.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adddemo.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/textParam1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:hint="@string/param1"/>

<EditText
    android:id="@+id/textParam2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/textParam1"
    android:layout_below="@id/textParam1"
    android:layout_marginTop="27dp"
    android:hint="@string/param2"/>

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textParam2"
    android:layout_below="@+id/textParam2"
    android:layout_marginLeft="89dp"
    android:layout_marginTop="46dp"
    android:text="@string/Button"
    android:onClick="onClick" />

</RelativeLayout>

ActivityMain.java code is as below:

package com.example.adddemo;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity 
{

public static String rslt = "";

@Override
protected void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button)findViewById(R.id.btnAdd);

}

public void onClick(View view)
{
    final  AlertDialog ad=new AlertDialog.Builder(this).create();

    try
    { 
        EditText ed1=(EditText)findViewById(R.id.textParam1);
        EditText ed2=(EditText)findViewById(R.id.textParam2); 
        int value1=Integer.parseInt(ed1.getText().toString());
        int value2=Integer.parseInt(ed2.getText().toString());
        rslt="START";

        Caller call = new Caller(); 
        call.param1=value1;
        call.param2=value2; 
        //call.ad=ad;
        call.join(); 
        call.start();
        while(rslt=="START") 
        {
            try 
            {
                Thread.sleep(10); 
            }
            catch(Exception ex) 
            {
                rslt = ex.getMessage();
            }
        }
        ad.setTitle("RESULT OF ADD of "+value1+" and "+value2);
        ad.setMessage(rslt); 
    }
    catch(Exception ex) 
    {
        ad.setTitle("Error!"); ad.setMessage(ex.toString());
    }
    ad.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Caller.java class code is as below:
It will call the CallSoap class which will take care of calling the asp.net webservice

package com.example.adddemo;

public class Caller extends Thread
{
public CallSoap cs;
public int param1, param2; 

public void run()
{
    try
    {
        cs = new CallSoap();
        String resp=cs.Add(param1, param2);
        MainActivity.rslt = resp;
    }
    catch(Exception ex)
    {
        MainActivity.rslt=ex.toString();
    }    
}
}

CallSoap class code is as below It will deal with creating http request, binding the data into a soap envelope and pass it using http request

package com.example.adddemo;
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class CallSoap 
{
public final String SOAP_ACTION = "http://tempuri.org/Add";
public  final String OPERATION_NAME = "Add"; 
public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public  final String SOAP_ADDRESS = "http://192.168.8.1/AddDemo.asmx";
    //where 192.168.8.1 is my system ip
    //AddDemo.asmx is my webservice 

public CallSoap()
{

}

public String Add(int param1, int param2)
{
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    PropertyInfo pi=new PropertyInfo();

    pi.setName("param1");
    pi.setValue(param1);
    pi.setType(Integer.class);
    request.addProperty(pi);
    pi=new PropertyInfo();
    pi.setName("param2");
    pi.setValue(param2);
    pi.setType(Integer.class);
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;
    try
    { 
        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
    }
    catch (Exception exception)
    {
        response=exception.toString();
    }
    return response.toString();
}

}
Was it helpful?

Solution 2

Hey all i figured out why the exception was caused.The error above say java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject. It could not find Ksoap2 dependent classes needed.

Solution:
1.) In eclipse goto project -> properties -> java build path -> order and export and check the Ksoap2 jar check box. This will make it available for the project during run time.

This worked for me very well for the above exceptions.

OTHER TIPS

from eclipse choose file -> new -> others -> android project from existing code then choose ksoap2 source code

then go ksoap2 properties -> android then check is library it must be true

then in your project properties -> android in the library section add ksoap2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top