1 / 29

Android WebService

經濟部工業局 Android 智慧型手機程式設計實務應用班. Android WebService. 建國科技大學 資管系 饒瑞佶 2012/4. WebService. 需要 ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar 或 ksoap2-android-assembly-2.5.2-jar-with-dependencies_timeout1.jar Project Properties Java Build PathLibraries  Add External JARs

baird
Download Presentation

Android WebService

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 經濟部工業局 Android智慧型手機程式設計實務應用班 AndroidWebService 建國科技大學 資管系 饒瑞佶 2012/4

  2. WebService • 需要ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar或ksoap2-android-assembly-2.5.2-jar-with-dependencies_timeout1.jar • ProjectPropertiesJava Build PathLibraries Add External JARs • 需要設定DNS才可以連結有Domain Name的WebService dns解決方法 • adb shell • #getprop查看DNS設定 • [net.dns1]: [192.168.2.1] net.dns1 就是目前的設定 • setprop net.dns1 168.95.1.1 設定成可以用的 DNS

  3. 幾個重點 • 沒有參數的WebService • 有參數的WebService • DNS問題

  4. WebService • 現有可以被呼叫的WebService: • 攝氏與華氏轉換 • http://www.w3schools.com/webservices/tempconvert.asmx • 取得國家 • http://www.webserviceX.NET/country.asmx

  5. 攝氏與華氏轉換

  6. WebService • Step1:建立新專案HelloWS • Step2:main.xml中建立一個輸入框(EditText)、兩個TextView與一個按鈕(Button) EditText TextView TextView Button

  7. WebService • Step3:加入ksoap2這個外部jar • Step4:宣告webservice參數 //有參數值的Web Service ---攝氏與華氏轉換(AVD無法解析domain name) private static final String NAMESPACE = "http://tempuri.org/" ; private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; private static final String METHOD_NAME = "CelsiusToFahrenheit"; private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit"; private EditTextet_pramater; // 輸入框 private Button btn_ok; // 按鈕 private TextViewtv_msg; // 訊息框

  8. 宣告webservice參數

  9. WebService • Step5:建立物件

  10. webservice主體 相對位置

  11. WebService • Step6:撰寫webservice主體 // webservice public String tempconvert(String aa){ String receivedString1="nok"; //預設回傳值 try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("Celsius", aa); //傳入帳號 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); envelope.dotNet = true; HttpTransportSEandroidHttpTransport = new HttpTransportSE(URL,TimeOut); androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive Result = (SoapPrimitive)envelope.getResponse(); receivedString1=Result.toString(); }catch(Exception e) { receivedString1="nok"; return receivedString1; } return receivedString1; } //---------------------------tempconvert--------------------------

  12. 呼叫tempconvert

  13. 呼叫tempconvert @Override public void onClick(View v) { // TODO Auto-generated method stub String to_be_transfered; to_be_transfered=et_pramater.getText().toString(); String value_return; if(to_be_transfered==null || "".equals(to_be_transfered)){ tv_msg.setText("您沒有輸入轉換值"); et_pramater.setFocusable(true); }else{ value_return=tempconvert(to_be_transfered); if(value_return=="nok"){ tv_msg.setText("轉換失敗"); }else{ tv_msg.setText(value_return); } } }

  14. 相對位置

  15. 執行結果

  16. 執行結果

  17. 錯誤原因 • 要開放網路存取權限(Manifest.xml) • <uses-permission android:name="android.permission.INTERNET"></uses-permission> • 要設定模擬器的DNS • 允許使用jar

  18. 設定模擬器DNS

  19. 執行結果

  20. 執行結果驗證

  21. And suggestion or improvement?

  22. My Suggestion • 試試另一個取得國家代號的webservice • 選用只能輸入數字的EditText • 加入等待訊息或進度畫面 • 將結果帶到下一個Activity做顯示 • 回傳結果不只一筆,分割資料後,再用ListActivity做顯示 • 將結果存入SQLite • …

  23. 整合ListView • 無參數 • 回傳一組以,為分隔符號的結果 private static final String NAMESPACE = "http://tempuri.org/" ; private static final String URL = "http://211.20.52.86/map_ap_1/city.asmx"; private static final String METHOD_NAME = "city"; private static final String SOAP_ACTION = "http://tempuri.org/city";

  24. 整合ListView 呼叫Web Service ListView

  25. 整合Spinner • WebService呼叫不變 要加一個有Spinner的layout Spinner

  26. Spinner sp=(Spinner)findViewById(R.id.spinner1); //建立一個ArrayAdapter物件,並放置下拉選單的內容 ArrayAdapter<String> adapter = new ArrayAdapter<String>( WS_Spinner.this,android.R.layout.simple_spinner_item, cities ); //設定下拉選單的樣式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter);

  27. 加入等待進度畫面

  28. 加入等待進度畫面

  29. private void createCancelProgressDialog(String title, String message, String buttonText) { MyDialog = new ProgressDialog(this); MyDialog.setTitle(title); MyDialog.setMessage(message); MyDialog.setButton(buttonText, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ // Use either finish() or return() to either close the activity or just the dialog MyDialog.dismiss(); return; } }); MyDialog.show(); }

More Related