webservice return使用soap返回Error而不是期望值

时间:2012-11-06 15:18:29

标签: android web-services

这是我用this video调用webservice编写的代码。当我运行它时,我收到一个错误字符串而不是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

1 个答案:

答案 0 :(得分:1)

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

您的代码:

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

真实代码:

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);