Search code examples
asp.netweb-servicessoapksoap2android-ksoap2

Error connecting Android App to ASP .NET Web Service using ksoap2


I am trying to connect my App to a Web Service that I created in ASP and is currently running on localhost. The App is trying to access the Coordonate method that takes in 3 integers.

I have provided the code and the LogCat. I have to mention that I'm new to web services.

Any help will be greatly appreciated! Cheers!

Code:

package Dan.Denver.Nuggets.googleMaps;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SlideBar extends Activity
{
SeekBar seekBar;
TextView textView;
Button sendButton;

private static final String SOAP_ACTION = "http://tempuri.org/Coordonate";
private static final String METHOD_NAME = "Coordonate";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://localhost:49934/Service1.asmx";

SoapObject request;
SoapSerializationEnvelope soapEnvelope;
HttpTransportSE transport;
SoapPrimitive rezultat;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slidebar);
    textView = (TextView)findViewById(R.id.textView); 
    sendButton = (Button)findViewById(R.id.bSB);

    seekBar = (SeekBar)findViewById(R.id.sbSB);
    seekBar.setMax(100);
    seekBar.setProgress(1);

    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    {   
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            // TODO Auto-generated method stub
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
            // TODO Auto-generated method stub
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) 
        {
            textView.setText(String.valueOf(progress + "%"));
//              if(progress >= 25 && progress < 50)
//                  textView.setTextColor(R.color.Yellow);
//              else if(progress >= 50 && progress < 75)
//                  textView.setTextColor(R.color.Orange);
//              else if(progress >= 75 && progress <= 100)
//                  textView.setTextColor(R.color.Red);
//              else 
//                  textView.setTextColor(R.color.Green);   
        }
    });

    sendButton.setOnClickListener(new View.OnClickListener() 
    {   
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            //Toast.makeText(getBaseContext(), "Drumul este congestionat in proportie de "+textView.getText().toString(), Toast.LENGTH_LONG).show();
            request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("latitudine", Main.latitudine);
            request.addProperty("longitudine", Main.longitudine);
            request.addProperty("procentTrafic", seekBar.getProgress());

            soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(request);

            transport = new HttpTransportSE(URL);
            try
            {
                transport.call(SOAP_ACTION, soapEnvelope);
                rezultat = (SoapPrimitive)soapEnvelope.getResponse();
                Toast.makeText(getBaseContext(), rezultat.toString(), Toast.LENGTH_LONG).show();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }           
        }
    });
}
}

LogCat:

07-01 17:11:56.790: E/AndroidRuntime(1029): FATAL EXCEPTION: main
07-01 17:11:56.790: E/AndroidRuntime(1029): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
07-01 17:11:56.790: E/AndroidRuntime(1029):     at Dan.Denver.Nuggets.googleMaps.SlideBar$2.onClick(SlideBar.java:80)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at android.view.View.performClick(View.java:2408)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at android.view.View$PerformClick.run(View.java:8816)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at android.os.Handler.handleCallback(Handler.java:587)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at android.os.Looper.loop(Looper.java:123)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at java.lang.reflect.Method.invoke(Method.java:521)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-01 17:11:56.790: E/AndroidRuntime(1029):     at dalvik.system.NativeStart.main(Native Method)
07-01 17:12:00.229: I/Process(1029): Sending signal. PID: 1029 SIG: 9

Solution

  • Try replacing localhost with IP address of your system in following line.

    private static final String URL = "http://localhost:49934/Service1.asmx";
    

    This might help you.

    As even if you are testing it on emulator running on same system then also both asp.net and android have different IP for localhost maybe 10.0.2.2 in some cases.