Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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]
Posted
Updated 13-Nov-12 13:23pm
v2
Comments
AndroidVivek 22-Nov-12 0:19am    
String URL = "http://127.0.0.1/Service1.asmx";

use local address 127.0.0.1 which is reserved loop back address
and in response change Soap Primitive to Soap Object

SoapPrimitive is use for getting a single string value
SoapObject for getting more than one value
 
Share this answer
 
Just Look at Ksoap2 Jar placed in android libs folder the logcat shows this error because enable to get class from the above location.Just rename this file to ksoap2.
 
Share this answer
 
Guys Here is a code, that 100% works. i find many solutions but all are crap.
First of all if you are using WCF then you must use .net framework 3.5 otherwise it will not work.
use AsyncTask<string,string,string> java class for AsyncTasks, because networking programs never work in main Thread.
Use this ip 10.0.2.2, others ip don't work. This ip is for android emulator. android virtually made this ip. Lan ip doesn't assign to android emulator.
Here is a code. And never forget to mention:
<uses-permission android:name="android.permission.INTERNET"/>
in manifest.xml.

package com.example.webservicesexample;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new Sync().execute("","","");
	}
	@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;
	}
}


package com.example.webservicesexample;

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

import android.content.AsyncTaskLoader;
import android.os.AsyncTask;
import android.util.Log;

public class Sync extends AsyncTask<String,String,String>{
	private final String SOAP_NAMESPACE="http://tempuri.org/";
	private final String SOAP_URL="http://10.0.2.2:17303/Service1.svc";
	private final String SOAP_ACTION="http://tempuri.org/IService1/GetData";
	private final String SOAP_METHOD_NAME="GetData";
	private SoapObject request;
	@Override
	protected String doInBackground(String... params) {
		request=new SoapObject(SOAP_NAMESPACE,SOAP_METHOD_NAME);
		PropertyInfo _info = new PropertyInfo();
		_info.setType(String.class);
		_info.setName("name");
		_info.setValue("qasim");
		request.addProperty(_info);
		SoapSerializationEnvelope envp = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envp.dotNet=true;
		envp.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_URL);
		try
		{
			androidHttpTransport.call(SOAP_ACTION, envp);
		}catch(Exception ex)
		{
			Log.d("Exception",ex.toString());
		}
		return null;
	}
}
 
Share this answer
 
v2
Solution 3 is total waste of time code is not working
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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