Android:About ksoap2 and webservice - android

I get the wsdl and the URL,and the server is written in C++;
I use KSoap2 in android to access the method ,but it always
prints out :"Method 'methodname' not implemented"!!!
Can anyone help me out?
Thanks in advance!

Are you creating your request and the SoapAction properly?
Try the example below (it is a public web service with 0 arguments) and see if it works for you.
private static final String WSDL_URL = "http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL";
private static final String WS_NAMESPACE = "http://ws.cdyne.com/WeatherWS/";
private static final String WS_METHOD_NAME = "GetWeatherInformation";
// 1. Creating SOAP request with no arguments
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(new SoapObject(WS_NAMESPACE, WS_METHOD_NAME));
// 2. Create a HTTP Transport object to send the web service request
HttpTransportSE httpTransport = new HttpTransportSE(WSDL_URL);
httpTransport.debug = true; // allows capture of raw request/respose in Logcat
// 3. Make the web service invocation
httpTransport.call(WS_NAMESPACE + WS_METHOD_NAME, envelope);
Log.d(TAG, "HTTP REQUEST:\n" + httpTransport.requestDump);
Log.d(TAG, "HTTP RESPONSE:\n" + httpTransport.responseDump);
Have a look at this detailed tutorial explaining the basics of using kSOAP2 with Android

see here the simple example of ksoap may help http://vimeo.com/9633556

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://216.128.29.26/webservices/TempConvert.asmx";
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit"
you should specify necessary field as above.

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://216.128.29.26/webservices/TempConvert.asmx";
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv;
tv = (TextView) findViewById(R.id.tv);
tv.setText(ws());
}
public String ws() {
String result = "";
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
if(envelope.getResponse()!=null){
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
result = "FeranHit : " + response.toString();
}
}
catch (Exception e)
{
result = e.getMessage();
}
return result;
}
see this is my code. and m getting full result. This is just test program for ksoap2.
i have not included any libraries here.

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.

Configure retrieved data from web service

public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/xxxx/GetLatLong";
private static final String METHOD_NAME = "GetLatLong";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://xxxx:xxxx/xxxx.svc/soap";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
}
catch (Exception exception)
{
textView.setText(exception.toString());
}
}
}
This is an example of my code.
This returns a string with 3 data description latitude and longitude .
I want to handle them separately how can I manage this ?
I want to put it then on a map with that configuration.
Write code something like this
JSONArray array=new JSONArray("your string response in json format");
for(int count=0;count<array.length();count++)
{
JSONObject obj=array.getJSONObject(count);
String description=obj.optString("Description");
String latitude=obj.optString("Latitude");
String longitude=obj.optString("Longitude");
// Logic to display latlong on map
}

Android Web Service connenction Error

I created Java web services and trying to connect with android code.I am getting the service running but android app is showing error in soap object creating for namespace and method name.I did all the changes and everything is correct namespace,method name,URL all is correct.But I don't know what is wrong can anyone help me out...!
My android code is----->
showing error in
-"SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);"
public class RetailerActivity extends Activity {
private static final String SOAP_ACTION = "urn:training/searchCompanyInfo";
private static final String METHOD_NAME = "searchCompanyInfo";
private static final String NAMESPACE = "urn:training/";
private static final String URL = "http://localhost/attest/CompanyInfoService?wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(NAMESPACE);
System.out.println(METHOD_NAME);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Android, You cannot perform network operation on the main thread, You need to execute your soap request in a background thread. Please read up on AsyncTask.

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.

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