如何在安卓系统中调用ksoap web服务?

5
我有一个小困惑,在我们的移动应用程序中,类似下面这样从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服务并将数据传递给它。但是每次都会得到服务器端错误代码:415,请有人帮助我并告诉我如何将这种层级数据传递给此类型的KSOAP Web服务。
我尝试了以下代码:
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)

请添加您的代码(Java代码,而不是iOS代码)和错误的堆栈跟踪,否则,请参考以下教程以了解如何使用ksoap2:http://mirnauman.wordpress.com/2012/10/18/android-calling-a-web-service-using-ksoap2-passing-values-to-a-web-service/ - Houcine
发布的代码看起来不像是Java代码,而更像与iOS相关的代码。 - Raghunandan
是的,@Raghunandan,实际上我已经发布了iOS代码,但我不确定如何在Android中使用它? - mvts himm
我已经发布了我的Android代码,请刷新您的页面。 - mvts himm
@user2753221 请查看此链接:https://dev59.com/dnDYa4cB1Zd3GeqPBIN2。您的代码看起来没问题。 - Raghunandan
显示剩余2条评论
1个回答

6
请尝试以下示例:
  1. ksoap-android-web-service-tutorial

  2. android-calling-a-web-service-using-ksoap2-passing-values-to-a-web-service

希望这能解决您的问题。
我使用以下方法调用ksoap web服务。
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;
}

非常感谢您的时间,@Khudabaks...但是我的代码有什么问题吗?你能告诉我吗? - mvts himm
你遇到的错误与不支持的媒体类型有关。当内容类型与 Web 服务要求的不同时,就会发生这种情况,以及您发送的类型是什么。 hconnection.setRequestProperty("Content-Type", "text/xml"); 因为我的 Web 服务正在获取 xml,所以我将内容类型设置为 xml。 - Khan
私有最终字符串URL =“http://brainteclabs.com:8180/MobileContractorCalculationService.svc”; - mvts himm
抱歉,但它不是本地网址,请检查此实时网址:http://brainteclabs.com:8180/MobileContractorCalculationService.svc - mvts himm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接