java - 使用 Rest 的 Android Web 服务 - SOAP 消息中出现错误

标签 java android web-services rest webservice-client

我在使用 Android RESTful 消费者从 Web 服务获取简单输出时遇到问题。下面是我的代码和输出。但是,我似乎无法从该网络服务返回值。 http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

如果有人能帮助我弄清楚为什么它在 SOAP 消息中这样说,我将不胜感激

"Server was unable to process request. ---> Data at the root level is      
invalid"

我的代码编译并运行良好

public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(
                "http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit");

        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("param1", "77"));
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);

        HttpResponse response = client.execute(request);

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String page = sb.toString();
        // Log.i(tag, page);
        System.out.println(page);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

我的输出

09-08 14:25:25.383: I/System.out(1620): <?xml version="1.0" encoding="utf-8"?>   
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code> 
<soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text   
xml:lang="en">Server was unable to process request. ---&gt; Data at the root level is  
invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault> 
</soap:Body></soap:Envelope>

09-08 14:25:25.863: D/gralloc_goldfish(1620): Emulator without GPU emulation detected.
09-08 14:30:00.076: I/Choreographer(1620): Skipped 35 frames!  The application may be doing too much work on its main thread.

最佳答案

我认为您应该按照您提供的网站的 HTTP POST 部分中的步骤进行操作。

据我所知,请求网址应该是http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit

此外,在您的情况下,POST 参数应为 Celsius=77

我用curl做了一个简单的测试:

curl -d "Celsius=50" http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit

我得到了回复:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">122</string>

看起来没问题。

在您的 onCreate() 方法中尝试一下:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);


        BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(
                    "http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit");

            String celsius = "50";
            request.addHeader("Content-Type", "application/x-www-form-urlencoded");

            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("Celsius", celsius));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);

            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String page = sb.toString();
            // Log.i(tag, page);
            System.out.println(page);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

其中 celsius 是您要 POST 的值。

我的输出(logcat)如下所示:

06-28 07:57:17.506: I/System.out(1847): <?xml version="1.0" encoding="utf-8"?>
06-28 07:57:17.506: I/System.out(1847): <string xmlns="http://tempuri.org/">122</string>

关于java - 使用 Rest 的 Android Web 服务 - SOAP 消息中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12333409/

相关文章:

java - 如何创建自定义二进制协议(protocol)服务器?

android - Eclipse Android : Error executing aapt: Return code 138, 我什么都没做似乎可以修复它

android - 当android中的滑动杀死应用程序时,如何调用web服务(Http请求)?

java - 是否可以在 JAR 中而不是在 WAR 中创建 JAX-WS 服务?

java - 覆盖 ClassNames 函数而不创建扩展 ClassName 的新类

java - 基于 MVC 使用线程的 Swing 应用程序

java - 如何将InputStream转换为字符串?

android - 如何在 firebase android 中获得特定的值(value)?

android - 如何使用 Android 在 Google map 中的两个位置之间绘制折线?

java - 如何提供自己的Web服务实现实例?