繁体   English   中英

如何从android调用ksoap Web服务?

[英]how to Call ksoap web service from android?

我有一个小小的困惑,在我们的移动应用程序中,从ios调用的ksoap Web服务如下所示:并正确获取数据。 在下面的Web服务中,您可以从以下代码中看到,有一些参数需要传递给Web服务。

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

在android方面的另一方面我已经尝试了很多方法来调用这个Web服务并将数据传递给Web服务。但每次我得到服务器端错误代码:415,请有人帮助我并告诉我如何将此分层数据传递给这种类型的ksoap网络服务....

我试过以下代码:

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

但它得到以下错误:

    09-09 14:33:18.054: V/(957): =========== Request : Calculate{inputRate=1000; rateFrequency=rateFrequency{RateFrequencyId=0; RateFrequencyName=Hourly; }; nonTaxableExpense=500; expenseFrequency=expenseFrequency{RateFrequencyId=0; RateFrequencyName=Hourly; }; }
09-09 14:33:30.264: W/System.err(957): java.io.IOException: HTTP request failed, HTTP status: 500
09-09 14:33:30.264: W/System.err(957):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195)
09-09 14:33:30.264: W/System.err(957):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
09-09 14:33:30.264: W/System.err(957):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
09-09 14:33:30.264: W/System.err(957):  at com.example.ksoapwebserviceexample.MainActivity.call(MainActivity.java:318)
09-09 14:33:30.264: W/System.err(957):  at com.example.ksoapwebserviceexample.MainActivity$testReq.doInBackground(MainActivity.java:185)
09-09 14:33:30.264: W/System.err(957):  at com.example.ksoapwebserviceexample.MainActivity$testReq.doInBackground(MainActivity.java:1)
09-09 14:33:30.274: W/System.err(957):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-09 14:33:30.274: W/System.err(957):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-09 14:33:30.274: W/System.err(957):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-09 14:33:30.274: W/System.err(957):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-09 14:33:30.274: W/System.err(957):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-09 14:33:30.274: W/System.err(957):  at java.lang.Thread.run(Thread.java:1096)

试试以下样品。

  1. KSOAP-Android的web服务教程

  2. Android的调用-A-Web服务-使用- ksoap2扯皮值到一个的Web服务

希望这能解决你的问题

我使用以下方法来调用ksoap web-servie

public String getServiceResponse(String nameSpace, String methodName,
        String soapAction, String Url, List<PropertyInfo> mPropertyInfo) {

    String mResponse = "";
    SoapObject request = new SoapObject(nameSpace, methodName);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    if (mPropertyInfo != null) {
        for (PropertyInfo propertyInfo : mPropertyInfo) {
            request.addProperty(propertyInfo);
        }
    }
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE ht = new HttpTransportSE(Url);
    ht.debug = true;
    try {
        ht.call(soapAction, envelope);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        mResponse = envelope.getResponse().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mResponse;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM