Click here to Skip to main content
15,886,794 members

How to send data from android app to asp.net web server using KSOAP

ahmed sherif shawkat asked:

Open original thread
I want to send data from the android app to the web server my code is as follows

after link the Ksoap.jar lib to my android app java build path

package com.ahmed.soapvideoservice;

C#
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.AndroidHttpTransport;

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

public class MainActivity extends Activity {
	String NAMESPACE = "http://vladozver.org/";
    String METHOD_NAME = "GetSumOfTwoInts";
    String SOAP_ACTION = "http://vladozver.org/GetSumOfTwoInts";
    String URL = "http://localhost:51305/Service1.asmx";
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView1);
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
	       
	       PropertyInfo pi = new PropertyInfo();
	       pi.setName("Operand1");
	       pi.setValue(2);
	       pi.setType(int.class);
	       Request.addProperty(pi);

	       PropertyInfo pi2 = new PropertyInfo();
	       pi2.setName("Operand2");
	       pi2.setValue(5);
	       pi2.setType(int.class);
	       Request.addProperty(pi2);
	       
	       
	       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	       envelope.dotNet=true;
	       envelope.setOutputSoapObject(Request);
	       
	       AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	       try
	       {
	           androidHttpTransport.call(SOAP_ACTION, envelope);
	           SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
	           tv.setText("result = "+result);
	       }
	       catch(Exception e)
	       {
	           e.printStackTrace();
	           
	       }

	       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


and here is my asp.net web server
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://vladozver.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public int GetSumOfTwoInts(int Operand1, int Operand2)
        {
            return Operand1 + Operand2;
        }

    }
}


I got an error in the android

11-14 01:59:41.150: E/dalvikvm(338): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.ahmed.soapvideoservice.MainActivity.onCreate
11-14 01:59:41.150: W/dalvikvm(338): VFY: unable to resolve new-instance 502 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/ahmed/soapvideoservice/MainActivity;
11-14 01:59:41.150: D/dalvikvm(338): VFY: replacing opcode 0x22 at 0x0012
11-14 01:59:41.160: D/dalvikvm(338): VFY: dead code 0x0014-0087 in Lcom/ahmed/soapvideoservice/MainActivity;.onCreate (Landroid/os/Bundle;)V
11-14 01:59:41.230: D/AndroidRuntime(338): Shutting down VM
11-14 01:59:41.230: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-14 01:59:41.240: E/AndroidRuntime(338): FATAL EXCEPTION: main
11-14 01:59:41.240: E/AndroidRuntime(338): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
11-14 01:59:41.240: E/AndroidRuntime(338): 	at com.ahmed.soapvideoservice.MainActivity.onCreate(MainActivity.java:29)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.os.Looper.loop(Looper.java:123)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at java.lang.reflect.Method.invoke(Method.java:507)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 01:59:41.240: E/AndroidRuntime(338): 	at dalvik.system.NativeStart.main(Native Method)


[edit]fixed code blocks[/edit]
Tags: Mobile Apps (Android), ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900