我有一个代码,它调用 www,tempuri.org 提供的网络服务。当我尝试运行应用程序时,应用程序意外停止并显示消息 - “不幸的是,MyTest 已停止”.. 这是我的 java 文件 -
MainActivity.java :-
package com.example.mytest;
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.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final String SOAP_ACTION = "http://tempuri.org/CelciusToFarenheit";
public static final String METHOD_NAME = "CelciusToFarenheit";
public static final String NAMESPACE= "http://tempuri.org/";
public static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
SoapEnvelope se;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.TextView01);
SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);
Request.addProperty("Celcius","32");
SoapSerializationEnvelope sse = new SoapSerializationEnvelope (se.VER11);
sse.dotNet=true;
sse.setOutputSoapObject(Request);
HttpTransportSE abt = new HttpTransportSE(URL);
try
{
abt.call(SOAP_ACTION,se);
SoapObject resultString=(SoapObject)sse.getResponse();
String resultData=resultString.getProperty(0).toString();
tv.setText("Status :"+resultString);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml :-
<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=".MainActivity" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
logcat如下 -
08-10 01:35:10.421: E/AndroidRuntime(1077): FATAL EXCEPTION: main
08-10 01:35:10.421: E/AndroidRuntime(1077): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
08-10 01:35:10.421: E/AndroidRuntime(1077): at com.example.mytest.MainActivity.onCreate(MainActivity.java:30)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.Activity.performCreate(Activity.java:5133)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.os.Looper.loop(Looper.java:137)
08-10 01:35:10.421: E/AndroidRuntime(1077): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-10 01:35:10.421: E/AndroidRuntime(1077): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 01:35:10.421: E/AndroidRuntime(1077): at java.lang.reflect.Method.invoke(Method.java:525)
08-10 01:35:10.421: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-10 01:35:10.421: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-10 01:35:10.421: E/AndroidRuntime(1077): at dalvik.system.NativeStart.main(Native Method)
我是使用 android 开发 Web 服务的新手。你能建议我吗,该怎么办?