日期:2014-05-16  浏览次数:20415 次

android用webservice连接sqlserver数据库

       以前做的东西,只要用数据库的都是在项目里自己重新做一份数据。但是这种方法是很不可取的,首先,手机内存不会很大,把数据表建在项目里无疑又增大了程序。这样一来手机的运行速度可想而知。其次,数据大的时候还是放在数据库比较合适,不仅方便而且可达到同步的效果。

       很多应用软件所依存的数据都是在数据库里,这时方便精简又可同步到数据库的方法只有连接数据库了。这里就是用webservice连接数据库即soap协议来达到获取数据库信息的目的。

       做了个小例子:

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://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 = "http://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 = "http://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) findViewB