我被导入了一个 WebService 应用程序,它被正确导入并正确启动,但是在运行时我得到了如下所示的异常。
05-31 15:12:04.969: E/AndroidRuntime(797): FATAL EXCEPTION: AsyncTask #1
05-31 15:12:04.969: E/AndroidRuntime(797): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 15:12:04.969: E/AndroidRuntime(797): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-31 15:12:04.969: E/AndroidRuntime(797): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.lang.Thread.run(Thread.java:856)
05-31 15:12:04.969: E/AndroidRuntime(797): Caused by: java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
05-31 15:12:04.969: E/AndroidRuntime(797): at com.prgguru.android.MainActivity.getFahrenheit(MainActivity.java:64)
05-31 15:12:04.969: E/AndroidRuntime(797): at com.prgguru.android.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:101)
05-31 15:12:04.969: E/AndroidRuntime(797): at com.prgguru.android.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
05-31 15:12:04.969: E/AndroidRuntime(797): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-31 15:12:04.969: E/AndroidRuntime(797): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-31 15:12:04.969: E/AndroidRuntime(797): ... 4 more
和MainActivity.java 包 com.prgguru.android;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
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.HttpTransportSE;
import com.example.webserviceactivity.R;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
private String TAG = "PGGURU";
private static String celcius;
private static String fahren;
Button b;
TextView tv;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Celcius Edit Control
et = (EditText) findViewById(R.id.editText1);
//Fahrenheit Text control
tv = (TextView) findViewById(R.id.tv_result);
//Button to trigger web service invocation
b = (Button) findViewById(R.id.button1);
//Button Click Listener
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Check if Celcius text control is not empty
if (et.getText().length() != 0 && et.getText().toString() != "") {
//Get the text control value
celcius = et.getText().toString();
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
} else {
tv.setText("Please enter Celcius");
}
}
});
}
public void getFahrenheit(String celsius) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("Celsius");
//Set Value
celsiusPI.setValue(celsius);
//Set dataType
celsiusPI.setType(double.class);
//Add the property to request object
request.addProperty(celsiusPI);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getFahrenheit(celcius);
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
tv.setText(fahren + "° F");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
tv.setText("Calculating...");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
}