MainActivity :--
package soap_web_services;
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.HttpTransportSE;
import com.example.web_service_soap.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private final String NAMESPACE = "http://www.w3schools.com/webservices/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION1 = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private final String METHOD_NAME1 = "CelsiusToFahrenheit";
private final String SOAP_ACTION2 = "http://www.w3schools.com/webservices/FahrenheitToCelsius";
private final String METHOD_NAME2 = "FahrenheitToCelsius";
private static String celcius;
private static String fahren;
String TAG="AMIT";
TextView t1,t2;
EditText edit1,edit2;
static boolean flag=false;
Button btncelcius,btnfarhen,btnclear;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t1=(TextView)findViewById(R.id.text1);
t2=(TextView)findViewById(R.id.text2);
edit1=(EditText)findViewById(R.id.edit1);
edit2=(EditText)findViewById(R.id.edit2);
btncelcius=(Button)findViewById(R.id.celcius);
btnfarhen=(Button)findViewById(R.id.farhen);
btnclear=(Button)findViewById(R.id.clear);
btnfarhen.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if (edit2.getText().length() != 0 && edit2.getText().toString() != "")
{
celcius=edit2.getText().toString();
flag=true;
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
}
else {
Toast.makeText(getApplicationContext(), "please enter fahrenheit value", Toast.LENGTH_LONG).show();
}
}
});
btncelcius.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if (edit1.getText().length() != 0 && edit1.getText().toString() != "")
{
fahren=edit1.getText().toString();
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
}else
{
Toast.makeText(getApplicationContext(), "please enter Celcius value", Toast.LENGTH_LONG).show();
}
}
});
btnclear.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
edit1.setText("");
edit2.setText("");
}
});
}
public void getFahrenheit(String celsius) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//Property which holds input parameters
PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("Celsius");
//Set Value
celsiusPI.setValue(celsius);
//Set dataType
celsiusPI.setType(double.class);
//Add the property to request object
request.addProperty(celsiusPI);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION1, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getCelcius(String fahren) {
//Create request
SoapObject request1 = new SoapObject(NAMESPACE, METHOD_NAME2);
//Property which holds input parameters
PropertyInfo fahrenPI = new PropertyInfo();
//Set Name
fahrenPI.setName("fahren");
//Set Value
fahrenPI.setValue(fahren);
//Set dataType
fahrenPI.setType(double.class);
//Add the property to request object
request1.addProperty(fahrenPI);
//Create envelope
SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope1.dotNet = true;
//Set output SOAP object
envelope1.setOutputSoapObject(request1);
//Create HTTP call object
HttpTransportSE androidHttpTransport1 = new HttpTransportSE(URL);
try {
Log.d("inside try block getcelcius","inside try block getcelcius");
//Invole web service
androidHttpTransport1.call(SOAP_ACTION2, envelope1);
Log.d("inside try block getcelcius line 3","inside try block getcelcius line 3");
//Get the response
SoapPrimitive response1 = (SoapPrimitive) envelope1.getResponse();
//Assign it to fahren static variable
celcius = response1.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
if(flag==true)
{
getFahrenheit(celcius);
}else
{
Log.d("inside do in background else part","inside do in background else part");
getCelcius(fahren);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
if(flag==true)
{
edit1.setText(fahren + "� F");
}else
{
edit2.setText(celcius + "� C");
Log.d("inside op post else part","inside op post else part");
}
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
if(flag==true)
{
edit1.setText("Calculating...");
}else
{
edit2.setText("Calculating...");
}
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
}
XML file :--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:text="farhenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:text="celcius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/innerLinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/celcius"
android:text="celcius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/farhen"
android:text="farhenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/clear"
android:text="clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Manifest file:--
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.web_service_soap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="soap_web_services.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
reference:-- androidHive.info
Comments
Post a Comment