using .net service in android application? - android

I am accessing wcf service from android code
The web service is http://www.nbrm.mk/klservice/kurs.asmx
My question is what are the arguments in the SoapObject constructor and in the call method from HttpTransportSE object??
Here is the code
public class ExchangeMoneyMKActivity extends Activity {
private static final String SOAP_ACTION = "http://nbrm.mk/callService";
private static final String METHOD_NAME = "callService";
private static final String NAMESPACE = "http://nbrm.mk/";
private static final String URL = "http://www.nbrm.mk/klservice";
TextView txt;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.txtView1);
callService();
}
private void callService() {
// TODO Auto-generated method stub
try{
SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
txt.setText(result.toString());
}
}

private static final String METHOD_NAME = "GetExchangeRate";
private static final String NAMESPACE = "http://www.nbrm.mk/klservice/";
private static final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
private static final String URL = "http://www.nbrm.mk/klservice/kurs.asmx?kurs";
METHOD_NAME is the name of the method I am calling from the web service, NAMESPACE is the domain + root folder from the url

Related

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
}

java.lang.IllegalArgumentException: size <= 0 using ksoap2 library for consuming the WCF .SVC SOAP Web service

Thanks in advance please forgive me if I am making any mistake while explaining the problem.
I am new TO SOAP technology. I want to call a WCF .net based SOAP web service the request for the SOAP will look something like below
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:IsServiceLive_V>
<!--Optional:-->
<tem:version>1</tem:version>
</tem:IsServiceLive_V>
</soapenv:Body>
</soapenv:Envelope>
and I am trying to make the request in the code like this
public class MainActivity extends Activity {
public static String SOAP_ACTION1="http://tempuri.org/IService1/IsServiceLive_V";
public static String SOAP_ACTION2="http://tempuri.org/IService1/IsServiceLive_V";
public static String METHOD_NAME1="IsServiceLive_V";
public static String METHOD_NAME2="IsServiceLive_V";
public static String NAMESPACE="http://tempuri.org/";
public static String URL="http://64.27.48.117:3340/service/Service1.svc?wsdl";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btnFar);
btnCel = (Button)findViewById(R.id.btnCel);
btnClear = (Button)findViewById(R.id.btnClear);
txtFar = (EditText)findViewById(R.id.txtFar);
txtCel = (EditText)findViewById(R.id.txtCel);
btnFar.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Initialize soap request + add parameters
try
{
new SoapResult(1).execute();
}
catch(NullPointerException e)
{
}
}
});
}
public class SoapResult extends AsyncTask<Void, Void, SoapObject>
{
int flag_status;
SoapObject final_result;
public SoapResult(int flag) {
// TODO Auto-generated constructor stub
flag_status=flag;
}
#Override
protected SoapObject doInBackground(Void... arg0) {
// TODO Auto-generated method stub
if(flag_status==1)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//Use this to add parameters
/* PropertyInfo property=new PropertyInfo();
property.setName("tmp");
property.setValue("GetRestaurantList");
request.addProperty(property);*/
request.addProperty("version", "1");
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
System.out.println(request.toString());
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
System.out.println("ok");
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
System.out.println(result.toString());
//SoapResult soap=(SoapResult)envelope.getResponse();
//Get the first property and change the label text
final_result=result;
} catch (Exception e) {
e.printStackTrace();
System.out.println("hiiii...");
}
return final_result;
}
#Override
protected void onPostExecute(SoapObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(flag_status==1)
//txtCel.setText(result.getProperty(0).toString());
if(flag_status==0)
{
// txtFar.setText(result.getProperty(0).toString());
}
}
}
and the SOAP response in SOAP format should look something like this
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<IsServiceLive_VResponse xmlns="http://tempuri.org/">
<IsServiceLive_VResult>true</IsServiceLive_VResult>
</IsServiceLive_VResponse>
</s:Body>
</s:Envelope>
but it is giving me exception java.lang.IllegalArgumentException: size <= 0
after the call
androidHttpTransport.call(SOAP_ACTION1, envelope);
The XML SOAP request and responses are generated using Soup UI tool and are working fine
just not getting where i am going wrong a little bit help would be appreciable..
Once I was having the same issue. There might be some problem with your service.
And use this
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
Instead of
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Got the answer for what i was looking for . for the WCF web services we have to create separate class for each method to get the data since it cannot be run from the code where you wanted the results . So if anyone has the problem that I faced please do remember that to consume the WCF web service in your android project Create a different class for each method for the web service .I am giving an simple example below
public class GetNearBy {
public static String HOTEL_NAME="hotel_name";
public static String NUMBER_OF_BRANCHES="branches";
public static String IMAGE1="image1";
public static String IMAGE2="image2";
public static String IMAGE3="image3";
public static String IMAGE4="image4";
public static String IMAGE5="image5";
public static String IMAGE6="image6";
public static String HAS_EVENT="HasEvent";
public static String HAS_OFFER="HasOffer";
public static String HAS_MENU="HasMenu";
public static String HAS_POPULARFOOD="HasPopularFood";
public static String ID="Id";
public static String IS_OPEN="IsOpen";
public static String LAT="LAT";
public static String LONG="LONG";
public static String LOCATION="Location";
public static String STARS="Stars";
public static String FACILITY_COUNT="facility_count";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "GetNearBy";
private static final String SOAP_ACTION = "http://tempuri.org/IService1/GetNearBy";
private static final String URL = "http://xxxxxx/service/Service1.svc/soap";
public static ArrayList<HashMap<String, String>> connectToSOAPService()
{
ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// creating SOAP envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// this should be true even in not .NET services
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
String resultValue = "";
try {
// calling an action
androidHttpTransport.call(SOAP_ACTION, envelope);
// receiving response
SoapObject response = (SoapObject) envelope.getResponse();
for(int i=0;i<response.getPropertyCount();i++)
{
HashMap<String, String> result=new HashMap<String, String>();
SoapObject ele1=(SoapObject)response.getProperty(i);
String Hotel_name=ele1.getProperty("BranchName").toString();
result.put(HOTEL_NAME, Hotel_name);
data.add(result);
}
}
catch(Exception e)
{
}
return data;
}
}
and just call it from where you need to fetch the result from the WCF web service like this
GetNearBy.connectToSOAPService();
try like this
final String NAMESPACE = con.getResources().getString(
R.string.NAMESPACE);
final String METHOD_NAME = "ItemsByCountry";
final String SOAP_ACTION = con.getResources().getString(
R.string.SOAP_ACTION)
+ METHOD_NAME;
final String URL = con.getResources().getString(R.string.URL);
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("userId", UserId);
Request.addProperty("countryName", countryName);
SoapSerializationEnvelope soapEnvelop;
soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
HttpTransportSE htp = new HttpTransportSE(URL);
htp.call(SOAP_ACTION, soapEnvelop);
response = (SoapObject) soapEnvelop.getResponse();

error on using Ksoap2

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

Android WebServices with ksoap2

i have a wsdl file it the website and i would like to get some services.
My question is: how to find those parameters:
SOAP_ACTION
METHOD_NAME
NAMESPACE
URL
Here is my code but I have the connexion problem:
public class TestBookSoapActivity extends Activity {
private static final String SOAP_ACTION = "http://api.mdpi.com/ws/GetVersionInfo";
private static final String METHOD_NAME = "GetVersionInfo";
private static final String NAMESPACE = "http://api.mdpi.com/ws/";
private static final String URL = "http://api.mdpi.com/ws/mdpi.wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
executeAppelSOAP();
}
private void executeAppelSOAP() {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("arg0", "10");
SoapSerializationEnvelope enveloppe = new SoapSerializationEnvelope(SoapEnvelope.VER11);
enveloppe.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);
try {
androidHttpTransport.call(SOAP_ACTION, enveloppe);
Object resultat = enveloppe.getResponse();
System.out.println("Version = " + resultat.toString());
} catch(Exception e) {
e.printStackTrace();
System.out.println("Problem");
}
}
}
Use Soap-Ui http://www.soapui.org/ to get answers to your problem. Then modify your android code accordingly...

Android:About ksoap2 and webservice

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.

Categories

Resources