ksoap(web services) example in android using SoapPrimitive

user2126191 picture user2126191 · Jun 15, 2013 · Viewed 14.7k times · Source
  1. this code is not giving me any errors or warning but is not working properly ,I did not know where is the problem,Please help me.

    package com.example.webtest;
    
    import org.ksoap2.SoapEnvelope;
    import android.util.Log;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    String UserFahrenheit;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button b=(Button) findViewById(R.id.button);
    final EditText Med=(EditText) findViewById(R.id.MedServTextView);
    final TextView Test=(TextView) findViewById(R.id.TestTextView);
    
    b.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            //try{
                UserFahrenheit=Med.getText().toString();
    
                //String mobile=getData(UserMRN.trim());
                 String NAMESPACE = "http://tempuri.org/";
                    String METHOD_NAME = "FahrenheitToCelsius";
                    String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
                    String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
    
                    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    
                    Request.addProperty("Fahrenheit",UserFahrenheit.trim());
    
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(Request);
    
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
        try{
                    androidHttpTransport.call(SOAP_ACTION, envelope);
    
                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                    String Celsius;
                    Celsius= String.valueOf(response.toString());
                   Test.setText(Celsius);
    
        }catch(Exception e){
            e.getMessage();
        }
    
    
    
        //  }catch(Exception e){
        //      e.getMessage();
        //  }
    
    
        }
    });
    
        }
    
    
    
    @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;
    }
    
    }
    

Answer

harmjanr picture harmjanr · Jun 15, 2013

Why are you using a SoapPrimitive? First of all, check if your response is an Error. Otherwise, get de responsebody from the response object by calling response.bodyIn:

SoapObject response = null;
if (envelope.bodyIn instanceof SoapObject) { // SoapObject = SUCCESS
    response = (SoapObject) envelope.bodyIn;
} else if (envelope.bodyIn instanceof SoapFault) { // SoapFault =
                                                    // FAILURE
    SoapFault soapFault = (SoapFault) envelope.bodyIn;
    throw new Exception(soapFault.getMessage());
}

Then, you can get properties from your response by calling response.getProperty("responsecode") for example.

Also, put the debug value of your HttpTransportSE on true while debugging:

transportSE.debug = true;