how to Call ksoap web service from android?
I have a small confusion, in our mobile app an ksoap web service called
from ios like below : and getting data properly. Here in the following web
service you can see from following code, there are some parameters need to
pass to the web service.
-(void)callwebservice{
NSString *soapMessage = [NSString stringWithFormat:
@"<s:Envelope
xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><Calculate
xmlns=\"http://tempuri.org/\"><inputRate>%@</inputRate><rateFrequency
xmlns:a=\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\"
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:RateFrequencyId>0</a:RateFrequencyId><a:RateFrequencyName>%@</a:RateFrequencyName></rateFrequency><nonTaxableExpense>%@</nonTaxableExpense><expenseFrequency
xmlns:a=\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\"
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:RateFrequencyId>0</a:RateFrequencyId><a:RateFrequencyName>%@</a:RateFrequencyName></expenseFrequency></Calculate></s:Body></s:Envelope>\n",txtinputrate.text,lblrate.text,txtexpenses.text,lblexpense.text
];
NSURL *url = [NSURL URLWithString:[NSString
stringWithFormat:@"%@MobileContractorCalculationService.svc",[MobileContractorAppDelegate
getstrIpAddress] ]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest
requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage
length]];
[theRequest addValue:@"text/xml; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[theRequest
addValue:@"http://tempuri.org/IMobileContractorCalculationService/Calculate"
forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage
dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if( theConnection ){
webData = [NSMutableData data] ;
}
}
And other side at android side i have tried so many ways to call this web
service and pass data to web service .But every time i getting server side
error code: 415, Please somebody help me and tell me how to pass this
hierarchical data to this type of ksoap web service....
I have tried following code :
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("inputRate", 1000);
SoapObject rate = new SoapObject(SOAP_ACTION, "rateFrequency");
PropertyInfo info = new PropertyInfo();
info.setName("RateFrequencyId");
info.setValue(0);
rate.addProperty(info);
PropertyInfo info2 = new PropertyInfo();
info2.setName("RateFrequencyName");
info2.setValue("Hourly");
rate.addProperty(info2);
request.addProperty("rateFrequency", rate);
request.addProperty("nonTaxableExpense", 500);
SoapObject exp = new SoapObject(SOAP_ACTION,
"expenseFrequency");
PropertyInfo info3 = new PropertyInfo();
info3.setName("RateFrequencyId");
info3.setValue(0);
exp.addProperty(info3);
PropertyInfo info4 = new PropertyInfo();
info4.setName("RateFrequencyName");
info4.setValue("Hourly");
exp.addProperty(info4);
request.addProperty("expenseFrequency", exp);
Log.v("", "=========== Request : " + request);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
Log.v("myApp",
"========================== Res" + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
But it getting following error :
09-09 14:31:03.364: V/(929): =========== Request : Login{inputRate=1000;
rateFrequency=rateFrequency{RateFrequencyId=0; RateFrequencyName=Hourly;
}; nonTaxableExpense=500;
expenseFrequency=expenseFrequency{RateFrequencyId=0;
RateFrequencyName=Hourly; }; }
09-09 14:31:05.314: W/System.err(929): java.io.IOException: HTTP request
failed, HTTP status: 415
09-09 14:31:05.324: W/System.err(929): at
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195)
09-09 14:31:05.324: W/System.err(929): at
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
09-09 14:31:05.324: W/System.err(929): at
org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
09-09 14:31:05.324: W/System.err(929): at
com.example.ksoapwebserviceexample.MainActivity.call(MainActivity.java:319)
09-09 14:31:05.324: W/System.err(929): at
com.example.ksoapwebserviceexample.MainActivity$testReq.doInBackground(MainActivity.java:186)
09-09 14:31:05.324: W/System.err(929): at
com.example.ksoapwebserviceexample.MainActivity$testReq.doInBackground(MainActivity.java:1)
09-09 14:31:05.324: W/System.err(929): at
android.os.AsyncTask$2.call(AsyncTask.java:185)
09-09 14:31:05.324: W/System.err(929): at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-09 14:31:05.324: W/System.err(929): at
java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-09 14:31:05.324: W/System.err(929): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-09 14:31:05.334: W/System.err(929): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-09 14:31:05.334: W/System.err(929): at
java.lang.Thread.run(Thread.java:1096)
No comments:
Post a Comment