如何使用kso​​ap2连接到Android应用程序项目的Web服务?

时间:2012-10-10 13:21:58

标签: java android xml web-services ksoap2

我遇到了如何使用kso​​ap2连接到Web服务的问题 我的代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;
import org.ksoap2.transport.HttpTransportSE;
import android.widget.TextView;


public class FirstScreen extends Activity {

private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_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.activity_first_screen);
    tv= (TextView)findViewById(R.id.TextView01);

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

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

    HttpTransportSE bah = new HttpTransportSE(URL);

    try
    {
        bah.call(SOAP_ACTION, soapEnvelope);
        SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
        tv.setText("status: "  + resultString);

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

}

}

它说应用程序MyTest(进程com.falafel.mytest))意外停止!! :(

我的错误:

10-10 13:14:06.690: E/dalvikvm(279): Could not find class  'org.ksoap2.serialization.SoapObject', referenced from method com.falafel.mytest.FirstScreen.onCreate
10-10 13:14:06.690: W/dalvikvm(279): VFY: unable to resolve new-instance 501 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/falafel/mytest/FirstScreen;
10-10 13:14:06.690: D/dalvikvm(279): VFY: replacing opcode 0x22 at 0x0012
10-10 13:14:06.690: D/dalvikvm(279): VFY: dead code 0x0014-005a in Lcom/falafel/mytest/FirstScreen;.onCreate (Landroid/os/Bundle;)V
10-10 13:14:06.870: W/Resources(279): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070000}
10-10 13:14:06.890: D/AndroidRuntime(279): Shutting down VM
10-10 13:14:06.890: W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-10 13:14:06.920: E/AndroidRuntime(279): FATAL EXCEPTION: main
10-10 13:14:06.920: E/AndroidRuntime(279): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
10-10 13:14:06.920: E/AndroidRuntime(279):  at com.falafel.mytest.FirstScreen.onCreate(FirstScreen.java:32)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.os.Looper.loop(Looper.java:123)
10-10 13:14:06.920: E/AndroidRuntime(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-10 13:14:06.920: E/AndroidRuntime(279):  at java.lang.reflect.Method.invokeNative(Native Method)
10-10 13:14:06.920: E/AndroidRuntime(279):  at java.lang.reflect.Method.invoke(Method.java:521)
10-10 13:14:06.920: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-10 13:14:06.920: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-10 13:14:06.920: E/AndroidRuntime(279):  at dalvik.system.NativeStart.main(Native Method)
 10-10 13:14:11.240: I/Process(279): Sending signal. PID: 279 SIG: 9

任何帮助都会得到满足,谢谢!

2 个答案:

答案 0 :(得分:0)

你做完了吗

项目>属性> Java构建路径>订单和出口>最后,勾选您导入的KSOAP2库。

此外,不要在UI线程上使用Web服务,考虑服务或AsyncTask

答案 1 :(得分:0)

在Android中使用ksoap2有时会遇到挑战。正如ThePCWizard正确建议最好使用AsyncTask在后​​台线程上执行所有与网络相关的任务。

您收到java.lang.NoClassDefFoundError可能表示您的Android项目无法识别ksoap.jar

假设您安装了最新的ADT version,则需要在Android项目的.jar文件夹中添加libs文件。之后,右键单击.jar文件 - >构建路径 - >添加到构建路径

清理项目并尝试运行该应用程序。它应该删除未找到的类定义错误。

我已经在很长一段时间内实现了类似的应用程序。这是link

希望它有所帮助!