Android 使用ksoap2调用soapweb服务。我的应用程序崩溃了

Android 使用ksoap2调用soapweb服务。我的应用程序崩溃了,android,web-services,soap,wsdl,Android,Web Services,Soap,Wsdl,我需要在我的项目中调用soap web服务。我遵循YouTube上的教程,使用ksoup2实现了这项任务。他们在教程中展示的应用程序运行良好。但是当模拟器上开始出现白色屏幕时,应用程序就会崩溃(说“不幸的是,我的测试已经停止”)。是LogCat 你能看看代码和logcat,告诉我出了什么问题吗 有了它,我该如何修复它 我看过一些教程,其中他们在URL中给出了WSDL的路径,在其他教程中给出了web服务所在位置的路径(如本教程中所示)。两者的区别是什么 代码:- package com.falaf

我需要在我的项目中调用soap web服务。我遵循YouTube上的教程,使用ksoup2实现了这项任务。他们在教程中展示的应用程序运行良好。但是当模拟器上开始出现白色屏幕时,应用程序就会崩溃(说“不幸的是,我的测试已经停止”)。是LogCat

  • 你能看看代码和logcat,告诉我出了什么问题吗 有了它,我该如何修复它
  • 我看过一些教程,其中他们在URL中给出了WSDL的路径,在其他教程中给出了web服务所在位置的路径(如本教程中所示)。两者的区别是什么
  • 代码:-

    package com.falafel.myTest;
    //ANDROID CLIENT OF SOAP WEB SERVICES. SAMPLE (YOUTUBE) CLIENT.
    import android.os.Bundle;
    import android.app.Activity;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import android.widget.TextView;
    
    
    public class FirstScreen extends Activity {
    
        private static final String SOAP_ACTION= "http://www.w3schools.com/webservices/CelsiusToFahrenheit"; //soapAction will point to where the namespace is, slash, the name of the method
        private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
        private static final String METHOD_NAME= "CelsiusToFahrenheit";
        private static final String URL= "http://www.w3schools.com/webservices/tempconvert.asmx";//This URL is very important. Make sure it is pointing to asmx, and not to wsdl, that is a complete URL to where the web service itself is; This is not a WSDL file.
    
        TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.textView1);
    
    
            //Soap Portion of this client program starts here. This is 1/3rd of the major functionality of this program, since there are three major portions.
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);//If we CelsiusToFahrenheit dint take any parameters, I would be done with my soap request by this line. But it does take a string parameter (as we say in the wsdl), so we need to package the required parameters into the Request. That is, next line!
            Request.addAttribute("Celsius", "32"); //Celcius is the name of the parameter required by the function. 32 --> we are giving a hardcoded value here for simplicity. | We have to add this line for each required parameter.
    
    
            //Here starts the 2nd out of 3 major portions of this program. This portion is the SoapSerializationEnvelope, which is an important piece of what needs to happen in ksoap to call web services inside of android.
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //An important thing in creating this object is specifying which version of the soap specification we want to use. This is version 1.1
                                                                                                        //soapEnvelope is an in and out object, which means that it has 2 types of information clusters, named bodyIn which contains information we send to web service while making the call (i.e. the xml going out), and named bodyOut which brings back, from the web service, the response of the call to the web service (that is the xml coming in).
            soapEnvelope.dotNet= true; //Remember the asmx in the URL, that is this service is based in ASP.Net, and not PHP or something, so you have to specify that. If you forget to specify that, you'll get some errors complaining that some of your objects are NULL. And the tricky part is that sometimes it will work, so yeah! If you are sure that yours is a dotNet based web service, always be safe by setting it to true, and if it is not dotNet based, you'll have to set it to nottrue
            soapEnvelope.setOutputSoapObject(Request); //Pass the packaged Request to SoapEnvelope itself. SetOutputSoapObject() assigns the object to the envelope as the outbound message for the soap call.
    
    
            //3rd of the 3 major parts of this program:- Transport (HTTP transport itself)
            HttpTransportSE hts= new HttpTransportSE(URL);
            try{   //try-catch block to actually make the call
                hts.call(SOAP_ACTION, soapEnvelope); //SOAP_ACTION contains where the web service is, appended with the function to be invoked. While all the information which needs to be sent to the web service is present (like Request) in the bodyIn portion of the soapEnvelope. After the call is successful (i.e. after the execution of this line, the bodyOut portion of this envelope will contain the response of the webservice.
                SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
                tv.setText("Status : " + resultString);
            }   
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    
    字符串资源:-

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">My Test</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
    
    </resources>
    
    
    我的测试
    设置
    你好,世界!
    
    清单文件:-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.falafel.myTest"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.falafel.myTest.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>
    
    </manifest>
    
    
    
    布局:-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".FirstScreen" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="TextView" />
    
    </RelativeLayout>
    
    
    
    按照以下步骤操作

  • 右键单击项目并选择属性
  • 单击左侧面板中的“Java构建路径”
  • 在右侧选中ksoap2-android-assembly-2.4
  • 您可以使用以下选项:-

    1.-Download the Ksoap jar file if you are not and paste in libs folder than right click of that and add build path 
    2.-and import all that class 
    3.-and give the network state change permission