ksoap(web services) example in android using SoapPrimitive
14,710
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;
Author by
Admin
Updated on July 26, 2022Comments
-
Admin almost 2 years
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; } }
-
Admin almost 11 yearsI am using SoapPrimitive becouse I return primitive value(String) from web services ,I also using SoabObject but it giving me NullPointerException in this statment SoapObject pii = (SoapObject)soap.getProperty(0).toString(); I did not know how to solve this problem so I swap this with SoapPrimitive
-
harmjanr almost 11 yearsDon't directly parse your response to a string. You need to get the body of your response with the method bodyIn of your envelope, instead of calling getResponse() from the envelope. What is the value bodyIn returns?
-
Admin almost 11 yearsI am using bodyIn but is not giving me any result