1

我正在尝试肥皂解析,但它不起作用。在我的应用程序中,我使用 1 个编辑文本、1 个按钮、1 个文本视图,运行应用程序后,它将显示输出窗口。但是在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**'
4

2 回答 2

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;
}

我认为问题在于您没有等待后台任务完成。

于 2013-06-19T07:46:47.683 回答
0

您是否对 Soap 对象使用外部库?检查您的库是否包含在最终 apk 中:项目属性 -> Java 构建路径 -> 选项卡顺序和导出并检查“Android Private Libraries”是否已选中。

于 2013-06-19T07:45:30.660 回答