Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Afternoon,

Here I use Android Handlers to perform the background task.To access SOAP based web service from Android using ksoap2 library.I have to follow the below steps to complete this task.

1.Add ksoap2 to the java build path of Android project.
2.Code for the MainActivity.java class
Java
package com.example.webservicecalling;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;



import com.example.webservicecalling.R;

public class MainActivity extends ActionBarActivity {
	
   	public final static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
	    public final static String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
	
	
	public static final String NAMESPACE = "http://tempuri.org/";
	
	private static final String METHOD = "CelsiusToFahrenheit";
	
	  
	private TextView temperatureTxt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
}

	private void init() {
		temperatureTxt = (TextView) findViewById(R.id.textView1);
		
		new ConversionAsyncTask().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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
     
	private class ConversionAsyncTask extends AsyncTask<void,> {
		private SoapPrimitive response;

		@Override
		protected Void doInBackground(Void... params) {
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			
			SoapObject request = new SoapObject(NAMESPACE, METHOD);
			soapEnvelope.setOutputSoapObject(request);
			
			soapEnvelope.dotNet = true;
			
			try {
				HttpTransportSE aht = new HttpTransportSE(URL);
				aht.call(SOAP_ACTION, soapEnvelope);
				request.addProperty("Celsius","32");
				response = (SoapPrimitive) soapEnvelope.getResponse();
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			
               return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			temperatureTxt.setText("Status:" + response);
			           
		}
	}
		 

}


3.Add internet permission to AndroidManifest before the application tag
HTML
<uses-permission android:name="android.permission.INTERNET" xmlns:android="#unknown" />

4.Code for the activity_main.xml
HTML
<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.webservicecalling.MainActivity" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"  />

</textview></relativelayout>


After running the application, web service response displayed as null in emulator. Could you please suggest me on this.i have to tried almost but not getting the out put.

Regards,

Ramesh.N
Posted
Updated 10-Nov-14 21:12pm
v2

1 solution

Java
public final static String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
public static final String NAMESPACE = "http://tempuri.org/";

change to
Java
<pre lang="java">public final static String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
public static final String NAMESPACE = "http://www.w3schools.com/webservices/";
 
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