以前做的東西,隻要用數據庫的都是在項目裡自己重新做一份數據。但是這種方法是很不可取的,首先,手機內存不會很大,把數據表建在項目裡無疑又增大瞭程序。這樣一來手機的運行速度可想而知。其次,數據大的時候還是放在數據庫比較合適,不僅方便而且可達到同步的效果。
很多應用軟件所依存的數據都是在數據庫裡,這時方便精簡又可同步到數據庫的方法隻有連接數據庫瞭。這裡就是用webservice連接數據庫即soap協議來達到獲取數據庫信息的目的。
做瞭個小例子:
佈局:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" tools:context=".MainActivity" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/name" android:layout_width="200dp" android:layout_height="wrap_content"/> <Button android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/search"/> </LinearLayout> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
建一個工具類SOAPUtil:
public class SOAPUtil { public static Object doTransport(final String wsdUrl, final String webMethod) { String nameSpace = "https://tempuri.org/";//一般都是默認的 SoapObject soapObject = new SoapObject(nameSpace, webMethod); // soapObject.addProperty(propertyInfo) System.out.println(); SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); soapSerializationEnvelope.bodyIn = soapObject; soapSerializationEnvelope.dotNet = true; soapSerializationEnvelope.setOutputSoapObject(soapObject); HttpTransportSE httpTransportSE = new HttpTransportSE(wsdUrl); String SOAP_ACTION = "https://tempuri.org/" + webMethod; //輸出soapAction System.out.println(SOAP_ACTION); try { httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope); System.out.println("調用結束"); //輸出響應 System.out.println(soapSerializationEnvelope.getResponse()); if (soapSerializationEnvelope.getResponse() != null) { SoapObject result = (SoapObject) soapSerializationEnvelope .getResponse(); //輸出結果 for (int i = 0; i < result.getPropertyCount(); i++) { System.out.println("result [" + i + "] = "+ result.getProperty(i).toString()); } return result; } } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } return null; } }
主要實現方法:
public class MainActivity extends Activity { private Button searchs; private TextView results; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchs=(Button) findViewById(R.id.search); results=(TextView) findViewById(R.id.result); searchs.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //服務器地址 String wsdUrl="https://192.168.1.195:88/service1.asmx"; //方法名 String method="SelectAll"; Object result=SOAPUtil.doTransport(wsdUrl, method); results.setText(result.toString()); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
以上這些是我們在客戶端這邊的必要步驟,除此之外還需要服務器給出接口(接口名即activity裡的方法名)。這裡我沒有寫接口,接口其實很簡單各種編程語言都可以,主要就是sql操作語句,寫完部署到服務器即可。
demo我放在資源裡,有興趣可以參考下。由於服務器是公司的隻有內部網可以用,但這個項目絕對可行。