I am trying to call a soap which looks like this in SOAPUI. It's having 4 parameter. url is - http://seycel.com.mx/ws/res2.php
Inputs are like this-
`<usuario xsi:type="xsd:string">1212121212</usuario>
<sms xsi:type="xsd:string">saldo</sms>
<palabra xsi:type="xsd:string">0439267236</palabra>
<fecha xsi:type="xsd:string">2015-05-20 20:10:10</fecha>`
I want to call this from android and fetch the return tag. What I am trying to do is like this -
private static final String SOAP_ACTION = "urn:recargas#saldo";
private static final String METHOD_NAME = "saldo";
private static final String NAMESPACE = "urn:recargas";
private static final String URL = "http://seycel.com.mx/ws/res2.php?wsdl";
private class UserRegistrationTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... values) {
SoapPrimitive result = null;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("palabra", "0439267236");// Parameter for Method
request.addProperty("usuario", "1212121212");// Parameter for Method
request.addProperty("sms", "saldo");// Parameter for Method
request.addProperty("fecha", "15-05-30 20:52:20");// Parameter for Method
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = (SoapPrimitive) envelope.getResponse();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return result.toString();
}
protected void onPostExecute(String result) {
Log.d("TAG", "value: " + result);
}
}
getting an error like this java.lang.String cannot be cast to org.ksoap2.serialization.SoapPrimitive
Related
I'm trying to connect to a external webservice from an Android app, but currently it crashes on the XML response, please help me notice if the call structure is built incorrectly or any the response code is crashed. Here is my code:
public class sri extends AsyncTask<String, String, String>
{
public final static String URL = "http://qa-suia.ambiente.gob.ec:8092/suiawebservices/SuiaServices?wsdl";
public static final String NAMESPACE = "http://client.ambiente.gob.ec/";
public static final String SOAP_ACTION_PREFIX = "/";
private static final String METHOD = "getRuc";
private String resp;
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
protected String doInBackground(String... param)
{
SoapObject request = new SoapObject(NAMESPACE,METHOD);
//String JsonString=Serializa();
request.addProperty("user","99999999");
request.addProperty("pass","xxxxxxxxxxxx");
request.addProperty("rucNumber","99999999999");
//sobre.dotNet=true;
sobre.setOutputSoapObject(request);
try
{
HttpTransportSE transporte=new HttpTransportSE(URL);
transporte.call(NAMESPACE+METHOD, sobre);
} catch (Exception e)
{
String msn="error";
return msn;
}
try {
if (sobre != null) {
SoapPrimitive response = (SoapPrimitive)sobre.getResponse();
resp=response.toString();
}
else
{
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
protected void onProgressUpdate(Integer... values)
{
}
public void onPreExecute()
{
}
public void onPostExecute(String result)
{
String resultado=result;
}
Here's working code for what I'm doing calling a .Net Service (It looks like you're not putting your request into an envelope):
private static SoapPrimitive callProcServiceForScalar(String serviceMethod, String storedProc, String params) throws Exception {
String NAMESPACE = "http://" + YourIPAddress + "/";
String METHOD_NAME = getMethodName(serviceMethod);
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "http://" + YourIP + "/YourService.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("sPassword", sYourPassword);
request.addProperty("sData", sServerDB);
request.addProperty("sSP_Name", storedProc);
request.addProperty("sParam", params);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Enable the below property if consuming .Net service
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
numberOfBytesTransmitted = numberOfBytesTransmitted + StringToBytes(request.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);
SoapPrimitive returnable = null;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
returnable = (SoapPrimitive)envelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Msg:" + e.getMessage() + "; SP:" + storedProc + "; Params: " + params + "; Method:" + METHOD_NAME);
}
return returnable;
}
here is my code for getting data from server.
public class POSDataCloud {
private static String SOAP_ACTION1 = "http://tempuri.org/GetDescription";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "GetDescription";
private static String URL = "http://182.160.99.115:2080/webservice1.asmx?WSDL";
public String GetPOSData(int POSID)
{
String ret="";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//request.addProperty("Id",POSID);
PropertyInfo property = new PropertyInfo();
{
property.name = "Id";
property.setNamespace(NAMESPACE);
property.type = PropertyInfo.STRING_CLASS;
property.setValue("3");
}
request.addProperty(property);
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.implicitTypes=true;
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)
{
ret=result.getProperty(0).toString();
}
else
{
ret="no response";
}
}
catch (Exception ex)
{
ex.printStackTrace();
ret=ex.getMessage();
}
return ret;
}
}
But I want to do it using asyncTaskLoader.How to do it?
Edit:Actually property.setValue is the PosId.how to pass posId in loadInBackground in asynctaskloader?I only need the implementation of loadInBackground method.i think it shold have posId parameter.But i can't get any example of asynctaskloader which takes parameter in loadInBackground method.Anybody helps me is greatly appreciated.
Here is this sample code (MainActivity):
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.text1);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
tv.setText("Status: " + resultString);
} catch (Exception e) {
tv.setText("Problem: " + e.toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
When I run this code I got this message Problem: android.os.NetworkOnMainThreadException.
In the manifest.xml added the
<uses-permission android:name="android.permission.INTERNET" />
line.
Here
is the whole android project.
Does someone have any idea how to change this code?
Thank you in advance for any help you can provide.
This exception is thrown when an android application attempts to perform a network operation on its main thread. You must run your code in AsyncTask. Your code will look something like this
You can declare resultString as an instance variable
class SoapTask extends AsyncTask<String, Void, Void> {
private Exception exception;
#Override
protected Void doInBackground(Void... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
resultString = "Status:" + (SoapPrimitive)soapEnvelope.getResponse();
} catch (Exception e) {
resultString = "Problem:" + e.toString();
}
}
protected void onPostExecute(Void result) {
tv.setText("Status: " + resultString);
}
}
Try this..
NetworkOnMainThreadException throws when you are performing network operation on its main thread.
So you have to run you network operations in AsyncTask
You can call AsyncTask like below
new SerializationConnection().execute(URL);
then SerializationConnection class
class SerializationConnection extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(urls[0]);
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
return "Status: " + resultString;
} catch (Exception e) {
return "Problem: " + e.toString();
}
}
protected void onPostExecute(String result) {
tv.setText(result);
}
}
You are making a server call in main thread.if your target version is 9
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
For Better way try to use AsynTask
I have written a program for communicating with a web service and get response value. But when i debug the programme i end with requestDump=null at the line androidHttpTransport.call(SOAP_ACTION, envelope); Can some one tell me the reason for the error and what can i do for this
public class WebService extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String result=getFarenheit(et.getText().toString());
//tv.setText(result+"°F");
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... arg0) {
celsius=et.getText().toString();
SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsuiusPI= new PropertyInfo();
celsuiusPI.setName("Celsius");
celsuiusPI.setValue(celsius);
celsuiusPI.setType(String.class);
request.addProperty("XMLMarks",celsuiusPI);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
SoapPrimitive response;
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try{
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String dump= androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this, response.toString(), 20).show();
Log.i("WebService output", response.toString());
System.out.println("WebService Response"+response.toString());
Object res= response.toString();
resultval=(String) res;
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h){
String result=h;
tv.setText(result+"°F");
}
}
}
Just replace your service AsyncTask with this new one and see result:
code:
private class service extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("Celsius", txtCel.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_ACTION, 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
// txtFar.setText(result.getProperty(0).toString());
res = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
tv.setText(result + "°F");
}
}
this is my web service .i want to fetch only name . but when i am running my application i am getting an error.
java.lang.NoClassDefFoundError:org.ksoap2.serilization.soapobject at my package name
public static String SOAP_ACTION1 = "http://tempuri.org/GetContact";
// private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
public static String NAMESPACE = "http://tempuri.org/";
public static String METHOD_NAME1 = "GetContact";
//private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://115.119.182.114/Rotaryclub/RotaryService.asmx";
TextView txt;
//ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.displaycontacts);
txt=(TextView) findViewById(R.id.textView11);
//CALL the web service method with the two parameters vname and nname
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
request.addProperty( "Cid","1");
request.addProperty("Position", "doctor");
request.addProperty("Name", "abhishek");
request.addProperty("ImageUrl", "test.jpg");
request.addProperty("PhoneNo", "4324434323");
request.addProperty("MobileNo", "4324434323");
request.addProperty("FaxNo", "43-434-34");
request.addProperty("EmailId", "abh22ishek#gmail.com");
request.addProperty("Address", "kalkere");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// Make the soap call.
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
// SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
// String strRes = result.toString();
if(result != null){
txt.setText("SOAP response:\n\n" + result.getProperty(2).toString());
}