简体   繁体   English

从android调用Soap webservice

[英]Calling Soap webservice from android

I am using the below process, 我正在使用以下流程,

1)Create a String template for a SOAP request and substitute user-supplied values at runtime in this template to create a valid request. 1)为SOAP请求创建String模板,并在此模板中在运行时替换用户提供的值以创建有效请求。 2) Wrap this string in a StringEntity and set its content type as text/xml 3) Set this entity in the SOAP request. 2)将此字符串包装在StringEntity中,并将其内容类型设置为text / xml 3)在SOAP请求中设置此实体。

and with the help of httppost I am posting the request, 在httppost的帮助下,我发布了请求,

I am using a demo webservice from w3schools.com 我正在使用w3schools.com的演示网络服务

url---> 网址--->

http://www.w3schools.com/webservices/tempconvert.asmx http://www.w3schools.com/webservices/tempconvert.asmx

What I have tried is, 我试过的是,

HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx");          
        StringEntity se;
        try {
            SOAPRequestXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:CelsiusToFahrenheit><!--Optional:--><tem:Celsius>30</tem:Celsius></tem:CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>";
            Log.d("request is ", SOAPRequestXML+"!!!");
            se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);


        se.setContentType("text/xml");  
        httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
        httppost.setEntity(se);  

        HttpClient httpclient = new DefaultHttpClient();
        BasicHttpResponse httpResponse = 
            (BasicHttpResponse) httpclient.execute(httppost);
        HttpEntity resEntity = httpResponse.getEntity();
        t.setText(EntityUtils.toString(resEntity));



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I am able to get the response in soapui, so surely the code is wrong because in emulator I am getting the output, 我能够在soapui中得到响应,所以代码肯定是错误的,因为在模拟器中我得到了输出,

"the server cannot service the request because the media type is unsupported". “服务器无法为请求提供服务,因为媒体类型不受支持”。

Am I passing the correct parameter in the constructor of HttpPost or am I making the correct xml request.I tried a lot but could not figure it out. 我是否在HttpPost的构造函数中传递了正确的参数,或者我正在制作正确的xml请求。我尝试了很多但是无法弄明白。

Thanks 谢谢

The only problem with your code is you are setting Header as, 您的代码唯一的问题是您将Header设置为,

httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");

instead of, 代替,

httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");

As you can see the request in the URL that is -> w3schoools , they are using, 正如您在URL中看到的请求 - > w3schoools ,他们正在使用,

Content-Type: text/xml; charset=utf-8

and you where not passing the same content type. 而你没有传递相同的内容类型。 So, it was giving you error as, 所以,它给你的错误,因为,

The server cannot service the request because the media type is unsupported. 服务器无法为请求提供服务,因为不支持媒体类型。

So, just change the Header and you will get the desired response. 因此,只需更改标题即可获得所需的响应。

I have written an article on How to Call Web Service in Android Using SOAP at c-sharpcorner.com. 我在c-sharpcorner.com上写了一篇关于如何在Android中使用SOAP调用Web服务的文章。

So many person get helped from that article. 这篇文章让很多人得到了帮助。 You can also download it and run. 您也可以下载并运行。 I will help you to understand how to use SOAP for web service. 我将帮助您了解如何使用SOAP进行Web服务。

Edit 编辑

Take a look at following links. 看看以下链接。 It has complex data handling with ksoap. 它使用kso​​ap进行复杂的数据处理。

Have you tried using the ksoap2 library for Android ? 您是否尝试过使用适用于Android的ksoap2库?

you can find it here, give it a shot : 你可以在这里找到它,试一试:

https://code.google.com/p/ksoap2-android/ https://code.google.com/p/ksoap2-android/

Hope this helps ! 希望这可以帮助 !

I have a hunch that the emulator android version and the phone version are different. 我有预感,模拟器的Android版本和手机版本是不同的。

But I have few suggestions. 但我提出的建议很少。 Use following: 使用以下:

httppost.setHeader("Accept-Charset","utf-8");
httppost.setHeader("Accept","text/xml,application/text+xml,application/soap+xml");

similarly, set content type as all of the above. 类似地,将内容类型设置为以上所有。

This way the html form is posting 123 celsius. 这样,html表单发布了123摄氏度。 No SOAP or envelops, just working:) 没有SOAP或信封,只是工作:)

    try {
        HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit");
        StringEntity se = new StringEntity("Celsius=123");
        httppost.setEntity(se);
        httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse httpResponse = new DefaultHttpClient().execute(httppost);
        HttpEntity resEntity = httpResponse.getEntity();
        return EntityUtils.toString(resEntity);
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }

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

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