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
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) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
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
<uses-permission android:name="android.permission.INTERNET" xmlns:android="#unknown" />
4.Code for the activity_main.xml
<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