java 从 android 调用 Soap 网络服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16374354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:47:49  来源:igfitidea点击:

Calling Soap webservice from android

javaandroidweb-servicessoap

提问by Ice Box

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. 2) Wrap this string in a StringEntity and set its content type as text/xml 3) Set this entity in the SOAP request.

1) 为 SOAP 请求创建一个 String 模板,并在此模板中在运行时替换用户提供的值以创建一个有效的请求。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

谢谢

采纳答案by CKmum

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.

同样,将内容类型设置为上述所有内容。

回答by Lalit Poptani

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.

因此,只需更改标题即可获得所需的响应。

回答by Chintan Rathod

I have written an article on How to Call Web Service in Android Using SOAPat c-sharpcorner.com.

我在 c-sharpcorner.com上写了一篇关于如何使用 SOAPAndroid 中调用 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.

看看以下链接。它使用 ksoap 进行复杂的数据处理。

回答by Dan Riza

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 !

希望这可以帮助 !

回答by Andrew

This way the html form is posting 123 celsius. No SOAP or envelops, just working:)

这样,html 表单的温度为 123 摄氏度。没有 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();
    }