2

这是我编写的使用此视频调用 web 服务的代码。当我运行它时,我收到一个错误字符串而不是 89.6。

package com.flafel.myTest;

import android.app.Activity;
import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.SoapEnvelope;
import android.widget.*;

public class FirstScreen extends Activity {
    /** Called when the activity is first created. */
    private static final String SQAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
    private static final String METMOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


                tv = (TextView) findViewById(R.id.textView1);

                SoapObject Request = new SoapObject(NAMESPACE, "Method_Name");
                Request.addProperty("Celsius", "32");

                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                soapEnvelope.dotNet = true;
                soapEnvelope.setOutputSoapObject(Request);

                HttpTransportSE aht = new HttpTransportSE(URL);
                aht.debug=true;


                try {

                    aht.call(SQAP_ACTION, soapEnvelope);

                    SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
                            .getResponse();
                    int count = resultString.getAttributeCount();
                    tv.setText("Status:" + resultString);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }       
}

输出是:

Hello World,FirstScreen!
Status:Error
4

1 回答 1

1

因为您没有将 method_name 写入自己的位置。

你的代码:

SoapObject Request = new SoapObject(NAMESPACE, "Method_Name");

真实代码:

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
于 2012-11-06T15:31:53.300 回答