Invoke web service from android application using soap object - android

I wrote a class called callSoap that invokes a web service (asmx) called test ,
the web method called GetData take integer as parameter and return list of strings
I defined
SOAP_ACTION
OPERATION_NAME
WSDL_TARGET_NAMESPACE
SOAP_ADDRESS
of my web service but the connection failed it and it did not return data
have anyone an idea about this ?
public final String SOAP_ACTION = "http://tempuri.org/GetData";
public final String OPERATION_NAME = "GetData";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "http://10.0.2.2:8216/test.asmx";
public String Call(int id)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("id");
pi.setValue(id);
pi.setType(Integer.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
}

Related

How to consume DataSet response from a SOAP service in Android?

I've been using SOAP based services in a lot of Android apps and they always work fine (string output usually). However, I've come across a service that accepts a DataSet and SQL Query as input and returns a DataSet as output.
From what I know of Android, there is no DataSet object type present. My question is, how do we utilize such a service in Android? Is it even possible to do so?
Here is a snapshot of the service:
Gladly for you, you can interface Android with ASP.NET so you can read the DataSET from your SOAP WebService
I'll give a snippet
public class CallSoap
{
public final String SOAP_ACTION = "http://tempuri.org/Add";
public final String OPERATION_NAME = "Add";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "http://yoururl/File.asmx";
public CallSoap()
{
}
public String Call(int a,int b)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("a");
pi.setValue(a);
pi.setType(Integer.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("b");
pi.setValue(b);
pi.setType(Integer.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
}
First SOAP_ACTION = namespace as seen in figure 1+function name;
OPERATION_NAME = name of the web method;
WSDL_TARGET_NAMESPACE = namespace of the webservice;
SOAP_ADDRESS = absolute URL of the webservice;

Android: calling a simple webservice method using ksoap2

I'm trying to call the method getWeather() of this webservice: http://www.webservicex.com/globalweather.asmx?WSDL
Here is my code:
public class ServiceCall {
private static final String NAMESPACE = "http://www.webserviceX.NET";
private static final String URL = "http://www.webservicex.com/globalweather.asmx";
public String prova(String citta){
final String SOAP_ACTION = "http://www.webserviceX.NET/GetWeather";
final SoapObject requestObject=new SoapObject(NAMESPACE,"GetWeather");
PropertyInfo pi = new PropertyInfo();
pi.setName("CityName");
pi.setValue(citta);
pi.setType(String.class);
requestObject.addProperty(pi);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
envelope.setOutputSoapObject(requestObject);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
String res="";
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.bodyIn;
res=response.getPropertyAsString("Body");
}catch(Exception e){Log.d("Prova",e.toString());}
Log.d("Prova", res);
return res;
}
}
But I get this Exception : java.io.IOException: HTTP request failed, HTTP status: 500
Where am I wrong?
private static final String NAMESPACE = "http://www.webserviceX.NET";
should be private static final String NAMESPACE = "http://www.webserviceX.com";
and final String SOAP_ACTION = "http://www.webserviceX.NET/GetWeather";
should be final String SOAP_ACTION = "http://www.webserviceX.com/GetWeather";
let me know if i am mistaken.

SoapObject in android application

Here is some fragment of my android application code PerformDownload.java
public class PerformDownload {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.0.2.2:4304/Service1.asmx";
public String GetContacts(String username) throws IOException, XmlPullParserException
{
// String result = null;
final String SOAP_ACTION = "http://tempuri.org/GetContacts";
final String METHOD_NAME = "GetContacts";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("username",username);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; // put this only if the web service is .NET one
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
KvmSerializable response= (KvmSerializable)envelope.bodyIn;
I am getting error on SoapObject,SoapSerializationEnvelope(SoapEnvelope.VER11); and so on.
Why the above code is not accessing soap protocol.

Webservices in android

I am new to android. In my application I tried to call SOAP web services, in that I can't understand what is meant
for(SOAP_Action,OperationName,WSDL_TARGET_NAMESPACE,SOAP_ADDRESS). The following is my full code
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
System.out.println("subbu="+request);
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
}
catch (Exception exception)
{
textView.setText(exception.toString());
}
}
}
Can anybody explain what thats purpose for. Give some link from which I can get idea.
SOAP_ACTION / NAMESPACE and METHODS can be found in the WSDL file of the target webservice !
Here is a sample code to send a SOAP request to a webservice :
public class SoapRequest {
private static final String SOAP_ACTION = "xxx";
private static final String METHOD_NAME = "xxx";
private static final String NAMESPACE = "xxx";
private static final String URL = "url of the webservice";
public static SoapObject soap() throws IOException, XmlPullParserException {
SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);
/* Here you can add properties to your requests */
PropertyInfo pi1 = new PropertyInfo();
pi1.name = "xxx";
pi1.type = String.class;
request.addProperty(pi1, "xxx");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject soapResult = (SoapObject) envelope.bodyIn;
return soapResult;
}

Can not call C# .net Web Service method from Android client

I am trying to call a web service from Android client using the ksoap library.
Here is my android code
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.16.0.230/WebService/Test.asmx";
TextView tv;
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", "zawoad");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
String result = (String)envelope.getResponse();
tv.setText(result);
} catch (Exception e) {
tv.setText("exception :" + e.getLocalizedMessage());
}
}
And here is my web service method which is written in Test.asmx file
[WebMethod]
public string HelloWorld(string name)
{
return "Hello World" + name;
}
When the androidHttpTransport.call(SOAP_ACTION, envelope); line is executed it throws the following exception
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG #2:44 in java.io.InputStreamReader#43e593c8)
Please help..
This is working code
private static final String SOAP_ACTION = "http://tempuri.org";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.16.0.230/WebService/Test.asmx?wsdl";
/*write ?wsdl only for local system testing*/
TextView tv;
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", "zawoad");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,20000);//Updated
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
String result = resultsRequestSOAP.toString();
tv.setText(result);
} catch (Exception e) {
tv.setText("exception :" + e.getLocalizedMessage());
}
}
The calling that you are performing will not happen.
what is the web service return type? We can pass the values and call that.

Categories

Resources