我正在尝试使用soap解析,但它不起作用。在我的应用程序中,我使用1个edittext,1个按钮,1个textview,运行应用程序后,它将显示输出窗口。但是在我在edittext中输入任何值并单击按钮后,它将出错。我不知道怎么解决,请问有谁帮助我, 我的MainActivity低于..
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 = "Sakthi";
private static String celcius;
private static String fahren;
Button b;
TextView tv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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 class AsyncCallWS extends AsyncTask<String, Void, integer> {
@Override
protected integer doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getFahrenheit(celcius);
return null;
}
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();
}
}
@Override
protected void onPostExecute(integer 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");
}
}'
logcat中的错误就像这样
'**06-19 07:20:22.613: E/AndroidRuntime(1525): FATAL EXCEPTION: AsyncTask #1
06-19 07:20:22.613: E/AndroidRuntime(1525): java.lang.RuntimeException: An error occured while executing doInBackground()
06-19 07:20:22.613: E/AndroidRuntime(1525): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-19 07:20:22.613: E/AndroidRuntime(1525): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.lang.Thread.run(Thread.java:856)
06-19 07:20:22.613: E/AndroidRuntime(1525): Caused by: java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
06-19 07:20:22.613: E/AndroidRuntime(1525): at com.example.medimixsoap.MainActivity$AsyncCallWS.getFahrenheit(MainActivity.java:74)
06-19 07:20:22.613: E/AndroidRuntime(1525): at com.example.medimixsoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:68)
06-19 07:20:22.613: E/AndroidRuntime(1525): at com.example.medimixsoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
06-19 07:20:22.613: E/AndroidRuntime(1525): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-19 07:20:22.613: E/AndroidRuntime(1525): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-19 07:20:22.613: E/AndroidRuntime(1525): ... 4 more**'
答案 0 :(得分:0)
您是否为Soap对象使用外部库?检查您的库是否包含在最终的apk中:项目属性 - &gt; Java构建路径 - &gt;选项卡顺序和导出并检查&#34; Android私有库&#34;检查。
答案 1 :(得分:0)
而不是:
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
尝试这个:
AsyncCallWS threadsStart = new AsyncCallWS();
threadsStart.execute(celcius);
try {
farenheit = threadsStart.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
然后在AsyncCallWS类中而不是:
@Override
protected integer doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getFahrenheit(celcius);
return null;
}
尝试这个:
@Override
protected integer doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getFahrenheit(params);
return null;
}
我认为问题来自于你不等待后台任务完成的事实。