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操作语句,写完部署到服务器即可。

    demo我放在资源里,有兴趣可以参考下。由于服务器是公司的只有内部网可以用,但这个项目绝对可行。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值