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) findViewById(R.id.result);  

        searchs.setOnClickListener(new View.OnClickListener() {  

               

            @Override 

            public void onClick(View v) {  

                //服务器地址  

                String wsdUrl="http://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操作语句,写完部署到服务器即可。

你可能感兴趣的:(webservice)