Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android WebService Android智慧型手機程式設計 建國科技大學 資管系 饒瑞佶 2012/4 V1 2012/8 V2

Similar presentations


Presentation on theme: "Android WebService Android智慧型手機程式設計 建國科技大學 資管系 饒瑞佶 2012/4 V1 2012/8 V2"— Presentation transcript:

1 Android WebService Android智慧型手機程式設計 建國科技大學 資管系 饒瑞佶 2012/4 V1 2012/8 V2

2 提醒… 這節的內容針對的是MS的Web Service或是使用SOAP(Simple Object Access Protocol)標準建立的Web Service 針對其它資料庫或是data provider,建議可以採用HTTPPost或是HttpGet

3 Why Web Service? 資訊/功能分享 標準(SOAP、XML、JSON) 安全 其他…

4 WebService 需要ksoap2-android-assembly jar-with-dependencies.jar或ksoap2-android-assembly 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]: [ ] net.dns1 就是目前的設定 setprop net.dns  設定成可以用的 DNS

5 幾個重點 沒有參數的WebService 有參數的WebService DNS問題

6 現有可以被呼叫的WebService: WebService 攝氏與華氏轉換 取得國家
取得國家

7 攝氏與華氏轉換

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

9 WebService Step3:加入ksoap2這個外部jar Step4:宣告webservice參數
//有參數值的Web Service ---攝氏與華氏轉換(AVD無法解析domain name) private static final String NAMESPACE = " ; private static final String URL = " private static final String METHOD_NAME = "CelsiusToFahrenheit"; private static final String SOAP_ACTION = " private EditText et_pramater; // 輸入框 private Button btn_ok; // 按鈕 private TextView tv_msg; // 訊息框

10 宣告webservice參數

11 WebService Step5:建立物件 et_pramater =(EditText) findViewById(R.id.editText1); btn_ok =(Button) findViewById(R.id.button1); tv_msg =(TextView) findViewById(R.id.textView2);

12 webservice主體 相對位置

13 WebService Step6:撰寫webservice主體
public String tempconvert(String temp){ String receivedString="not work"; //預設回傳值 try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("Celsius", temp); //傳入溫度 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); envelope.dotNet = true; HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive Result = (SoapPrimitive)envelope.getResponse(); receivedString=Result.toString(); }catch(Exception e) { receivedString="not work"; return receivedString; }

14 呼叫tempconvert

15 呼叫tempconvert btn_ok.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) { 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); //呼叫WS if(value_return=="not work"){ tv_msg.setText("轉換失敗"); tv_msg.setText(value_return); } });

16 相對位置

17 執行結果

18 執行結果

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

20 設定模擬器DNS

21 執行結果

22 執行結果驗證

23 And suggestion or improvement?

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

25 整合ListView 無參數 回傳一組以,為分隔符號的結果
private static final String NAMESPACE = " ; private static final String URL = " private static final String METHOD_NAME = "city"; private static final String SOAP_ACTION = "

26 整合ListView 呼叫Web Service ListView

27 code @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); String getbackcity=WScity(); //呼叫WS String [] cities= getbackcity.split(","); //利用,切割取得的字串變陣列 //設定利用ListView顯示 setListAdapter(new ArrayAdapter <String>(this,android.R.layout.simple_list_item_checked,cities)); getListView().setTextFilterEnabled(true); //設定可以篩選 // 取得listview ListView lv=this.getListView(); lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); //設定可勾選 //lv.setFilterText("台"); }

28 code public String WScity(){ String receivedString="nok"; //預設回傳值 try
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); envelope.dotNet = true; HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive Result = (SoapPrimitive)envelope.getResponse(); receivedString=Result.toString(); }catch(Exception e) { receivedString="nok"; return receivedString; }

29 android.os.NetworkOnMainThreadException
對網路存取增加了一些限制 不能在onCreate()方法中直接使用外部連結

30 解決方法 加入! @Override public void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .penaltyDeath() super.onCreate(savedInstanceState); ...... // 發送Http請求 } 加入!

31 加入!

32 整合Spinner WebService呼叫不變 要加一個有Spinner的layout Spinner

33 Spinner sp=(Spinner)findViewById(R.id.spinner1);
String getbackcity=WScity(); //呼叫WS String [] cities= getbackcity.split(","); //利用,切割取得的字串變陣列 //設定利用Spinner顯示 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainSpinner.this,android.R.layout.simple_spinner_item,cities ); //設定下拉選單的樣式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter);

34 加入等待進度畫面

35 加入等待進度畫面

36 private ProgressDialog MyDialog=null;
createCancelProgressDialog("擷取中","請稍待...","取消"); @SuppressWarnings("deprecation") 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();

37 有沒有興趣看看 HTTPPost與HTTPGet?


Download ppt "Android WebService Android智慧型手機程式設計 建國科技大學 資管系 饒瑞佶 2012/4 V1 2012/8 V2"

Similar presentations


Ads by Google