Android KSOAP2 call ASMX Web Service - android

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;

Related

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

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.

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

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