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

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.

Related

How to get NAMESPACE, SOAP_ACTION, URL and METHOD_NAME to call SOAP request in android

I have a url for calling in code I should call it with Ksoap2 library in code.
My code is in below,
final String NAMESPACE ="";
final String URL ="";
final String METHOD_NAME = "";
final String SOAP_ACTION = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(HoldPayment.Amount, "1000");
request.addProperty(HoldPayment.CallbackURL,"http://www.yoursoteaddress.ir/verify.php");
request.addProperty(HoldPayment.Description,"pule kharide tala");
request.addProperty(HoldPayment.Email,"za#gmail.com");
request.addProperty(HoldPayment.MerchantID,"e579752a-a591-11e6-9304-000c295eb8fc");
request.addProperty(HoldPayment.Mobile,"09012345678");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION,envelope);
Object resultsRequestSOAP = envelope.bodyIn;
Log.e("","Response::"+resultsRequestSOAP.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error"+e);
}
My url is,
https://www.zarinpal.com/pg/services/WebGate/wsdl
I don't know what I should set to namespace, method, action_soap and url in my code.
Try this,
private static final String NAMESPACE ="http://zarinpal.com/";
private static final String WSDL ="https://www.zarinpal.com/pg/services/WebGate/service";
private static final String METHOD_NAME = "PaymentRequest";
private static final String SOAP_ACTION = WSDL + "#" + METHOD_NAME;
private static String TAG = "soap";
public static String callWebservice() {
String responseDump = "";
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(HoldPayment.Amount, "1000");
request.addProperty(HoldPayment.CallbackURL,"http://www.yoursoteaddress.ir/verify.php");
request.addProperty(HoldPayment.Description,"pule kharide tala");
request.addProperty(HoldPayment.Email,"za#gmail.com");
request.addProperty(HoldPayment.MerchantID,"e579752a-a591-11e6-9304-000c295eb8fc");
request.addProperty(HoldPayment.Mobile,"090123456789");
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(WSDL);
transport.debug = true;
try {
transport.call(SOAP_ACTION, envelope);
String requestDump = transport.requestDump;
responseDump = transport.responseDump;
Log.e(TAG, requestDump);
Log.e(TAG, responseDump);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return responseDump;
}
This is how I found NAMESPACE, WSDL, METHOD_NAME and SOAP_ACTION.
NAMESPACE : Search for "targetNamespace" in the WSDL.
WSDL/URL : Search for "soap:address" in the WSDL. The value in location is the URL.
METHOD_NAME : I look at the arguments you were using to create the request. It had Amount, CallbackURL, Description, Email, MerchantID and Mobile (no AdditionalData). So I figured you are trying to call PaymentRequest method.
SOAP_ACTION : Search for "soapAction" in the WSDL. Among the matches, look for the one related to PaymentRequest. The SOAP_ACTION is usually the URL + some_seperator + METHOD_NAME. The separator in this case was #.
And so I found everything that was required to make the request. Hope it helped you. Good luck.

Ksoap2 Trouble consuming Webservices on Android

I created a webServices in .net but there is a trouble when I am consuming it from my Android app. I noticed that the webservice is not getting the parameters I send from Android. however, I consumed it from the .net app and it works ok.
Note: with another webservice (webservicex/globalweather .asmx) my Android app works perfectly.
I put some spaces in the string URL variable because I can't publish more than 2 links
public class cargaDatosWS {
private static final String SOAP_ACTION = "http://www.esmerlinp.somee.com/getAccess";
private static final String METHOD_NAME = "getAccess";
private static final String NAMESPACE = "http://www.esmerlinp.somee.com";
private static final String URL = "http ://www. esmerlinp.somee .com/Registro.asmx";
public String getAcess(String Email,String Pwd){
String res=null;
SoapObject rpc;
rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("email", Email);
rpc.addProperty("password", Pwd);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
HttpTransportSE androidHttpTransport= null;
try {
androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
res = envelope.getResponse().toString();
}catch (Exception e){
System.out.println(e.getMessage());
res=e.getMessage();
}
return res;
}
}

Android KSOAP2 call ASMX Web Service

class wsCall extends AsyncTask {
private static final String SOAP_ACTION = "http://tempuri.org/PrivateLogin";
private static final String METHOD_NAME = "PrivateLogin";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.110/omoffice/service/wsomjobs.asmx";
private Context mContext;
public wsCall (Context context){
mContext = context;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
protected String doInBackground(String... params)
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userid","poma");
request.addProperty("pw","1");
request.addProperty("azienda","1");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.bodyIn;
return "OK";
In androidHttpTransport.call(SOAP_ACTION, envelope) I obtain this error: java.net.ProtocolException: unexpected end of stream
Someone help me?
thanks
Marco
Are you sure if SOAP version you are using is 10??
try using SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); instead of SoapObject response = (SoapObject) envelope.bodyIn;

Invoke web service from android application using soap object

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();
}
}

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.

Categories

Resources