2013-06-19 121 views
1

我正在嘗試soap解析,但它不起作用。在我的應用程序中,我使用1個edittext,1個按鈕,1個textview,運行應用程序後,它將顯示輸出窗口。但是當我在edittext中輸入任何值並點擊按鈕後,就會出錯。我DONO如何解決,將玉請人幫我,我 是MainActivity下面..執行doinbackground()asynctask時出現錯誤

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

清理項目並再次運行它,它有noclassdefinition發現錯誤。它在運行時無法找到.class文件 – abhi

+1

celsiusPI.setType(double.class);可能你的意思是寫celsiusPI.setType(Double.class); – Blackbelt

+0

試試這個鏈接http://stackoverflow.com/questions/17020176/java-lang-noclassdeffounderror-com-applovin-sdk-applovinsdk/17020252#17020252 –

回答

0

你使用外部庫的SOAP對象?檢查您的庫包含在最終的apk中:項目屬性 - > Java Build Path - > Tab Order and Export並檢查是否選中了「Android Private Libraries」。

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

我認爲這個問題來自於您並未等待後臺任務完成。

相關問題