Mushtaq Ali

Mushtaq Ali

  • NA
  • 5
  • 712

SOAP Webservices

Jul 17 2016 7:15 AM
I am using Soap Webservices. I am a fresh learner and tried to write a code for SOAP Webservice. A very common example widely used on internet for practice " Celcius to Fehrenheit" and Fehrenheitto Celcius".  I am going to attach the code below. The same codes work perfectly in Eclipse while in android studio / Intellij it does not work. why? 
 
 
package com.example.user.soap_offloading;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private static String SOAP_ACTION1="http://www.w3schools.com/xml/CelsiusToFahrenheit";
private static String SOAP_ACTION2="http://www.w3schools.com/xml/FahrenheitToCelsius";
private static String NAME_SPACE="http://www.w3schools.com/xml/";
private static String METHOD_NAME1="CelsiusToFahrenheit";
private static String METHOD_NAME2="FahrenheitToCelsius";
private static String URL="http://www.w3schools.com/xml/tempconvert.asmx";

Button enter, enter1;
TextView tv1, tv2, tv3;
EditText et1, et2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clr=(Button)findViewById(R.id.button2);
enter=(Button)findViewById(R.id.button);
tv1=(TextView)findViewById(R.id.textView);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
et1=(EditText)findViewById(R.id.editText);
et2=(EditText)findViewById(R.id.editText2);

enter.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View view) {
 
 SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME1);

//Use this to add parameters
request.addProperty("Fahrenheit",et1.getText().toString());

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
envelope.dotNet = true;

try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);

// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;

if(result != null)
{
//Get the first property and change the label text
et2.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});




enter1.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View view) {
 SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME1);

//Use this to add parameters
request.addProperty("Fahrenheit",et2.getText().toString());

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
envelope.dotNet = true;

try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);

// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;

if(result != null)
{
//Get the first property and change the label text
et1.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});



clr.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
et1.setText("");
et2.setText("");
}
});
}
}

Answers (4)