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

很多应用软件所依存的数据都是在数据库里,这时方便精简又可同步到数据库的方法只有连接数据库了。这里就是用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操作语句,写完部署到服务器即可。

最新文章

  1. ANDROID 应用退出
  2. tomcat 部署spring工程乱码解决方案
  3. delphi关闭程序Close,application.Terminate与halt区别
  4. n 后问题
  5. 在html中注释对 &lt;include XXXXXXXX /&gt;是没有影响的
  6. linux下动态库编译的依赖问题
  7. 【转】android:layout_gravity和android:gravity的区别
  8. Elasticsearch 搜索不到数据问题(_mapping 设置)
  9. Python开发【第二十二篇】:Web框架之Django【进阶】
  10. android水平循环滚动控件
  11. Asp.net MVC4 +EF6开发
  12. 原生JavaScript实现焦点图轮播
  13. FFPLAY的原理(四)
  14. Java 图片爬虫,java打包jar文件
  15. webpack打包和gulp打包工具详细教程
  16. 状态压缩dp小结
  17. day85
  18. 关于thinkphp中post提交空白的思考
  19. Hadoop运维手记
  20. Android ImageSpan的使用

热门文章

  1. linux常用命令搜索
  2. C#中判断bool 类型 代码的最短写法
  3. .where(provider).FirstOrDefault()和.FirstOrDefault(provider)的性能比较
  4. mooc
  5. Sprint5
  6. phpStorm 配置关联php手册
  7. SQL中EXISTS和IN用法
  8. Chapter 32:动态规划一 总结
  9. MySQL优化器cost计算
  10. bzoj1079