290 likes | 486 Views
經濟部工業局 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 PathLibraries Add External JARs
E N D
經濟部工業局 Android智慧型手機程式設計實務應用班 AndroidWebService 建國科技大學 資管系 饒瑞佶 2012/4
WebService • 需要ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar或ksoap2-android-assembly-2.5.2-jar-with-dependencies_timeout1.jar • ProjectPropertiesJava Build PathLibraries 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
幾個重點 • 沒有參數的WebService • 有參數的WebService • DNS問題
WebService • 現有可以被呼叫的WebService: • 攝氏與華氏轉換 • http://www.w3schools.com/webservices/tempconvert.asmx • 取得國家 • http://www.webserviceX.NET/country.asmx
WebService • Step1:建立新專案HelloWS • Step2:main.xml中建立一個輸入框(EditText)、兩個TextView與一個按鈕(Button) EditText TextView TextView Button
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; // 訊息框
WebService • Step5:建立物件
webservice主體 相對位置
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--------------------------
呼叫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); } } }
錯誤原因 • 要開放網路存取權限(Manifest.xml) • <uses-permission android:name="android.permission.INTERNET"></uses-permission> • 要設定模擬器的DNS • 允許使用jar
My Suggestion • 試試另一個取得國家代號的webservice • 選用只能輸入數字的EditText • 加入等待訊息或進度畫面 • 將結果帶到下一個Activity做顯示 • 回傳結果不只一筆,分割資料後,再用ListActivity做顯示 • 將結果存入SQLite • …
整合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";
整合ListView 呼叫Web Service ListView
整合Spinner • WebService呼叫不變 要加一個有Spinner的layout Spinner
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);
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(); }