Android调用Web服务返回“错误”

时间:2011-05-14 14:37:23

标签: android web-services ksoap2

我从Android调用了Web服务:

.java文件:

public class FirstScreen extends Activity {


        private static final String NAMESPACE = "http://tempuri.org";
        private static final String URL = "http://w3schools.com/webservices/tempconvert.asmx";  
        private static final String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
        private static final String METHOD_NAME = "FahrenheitToCelsius";

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

            AutoCompleteTextView ACTV = (AutoCompleteTextView)findViewById
                            (R.id.autoCompleteTextView1);       

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            request.addProperty("Fahrenheit", "89");

            SoapSerializationEnvelope envelope = 
                new SoapSerializationEnvelope(SoapEnvelope.VER11); 

            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);           
                ACTV.setHint("Received :" + envelope.getResponse());
            } 
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mobitour.mytest"
      android:versionCode="1"
      android:versionName="1.0">    
   <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".FirstScreen"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     </application>    
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>     
    <uses-sdk android:minSdkVersion="8" />  
</manifest>

我在编写应用程序时遵循了这个http://www.vimeo.com/9633556。 但是我有一个问题:我收到一个单词“Error”而不是必需的字符串,我在屏幕上看到“Received:Error”。我无法理解哪里出错了!你呢?

1 个答案:

答案 0 :(得分:2)

我解决了

非常简单,需要

envelope. dotNet = true;

envelope.setOutputSoapObject(request);