Android网络编程之Web Service初步(客户端)
本程序需要在Android项目中导入KSOAP包,注意要放入lib文件夹内
客户端基本流程:定义好命名空间NAMESPACE, URL,SOAP_ACTION, 调用服务方法名等数据 --->
实例化SoapObject ---> 实例化SoapSerializationEnvelope 设置其bodyOut等参数 ---> 实例化HttpTransportSE 调用call方法实现web服务
---> 从SoapSerializationEnvelope取回服务器端返回的数据
Activity的部署与通信所需基本数据的定义:
1 public class MainActivity extends Activity { 2 3 private EditText enterText = null; 4 private Button saveBtn = null; 5 private Button loadBtn = null; 6 private TextView loadText = null; 7 8 // 此处注意命名空间为服务器端包名称 9 private static final String NAMESPACE = "http://moka.com" ; 10 private static String URL = "http://192.168.1.101:8080/WebServiceTest/services/FileServiceImpl" ; 11 private static final String SAVE_METHOD_NAME = "save" ; 12 private static final String LOAD_METHOD_NAME = "load" ; 13 private static String SOAP_ACTION = "http://192.168.1.101:8080/WebServiceTest/services" ; 14 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout. activity_main); 20 21 /*下面两段代码使Android3.0以上系统可以让 http代码使用主UI线程,因为3.0以上系统对UI资源的使用更严格*/ 22 StrictMode. setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 23 .detectDiskReads().detectDiskWrites().detectNetwork() 24 .penaltyLog().build()); 25 26 StrictMode. setVmPolicy(new StrictMode.VmPolicy.Builder() 27 .detectLeakedSqlLiteObjects().penaltyLog() 28 .penaltyDeath().build()); 29 30 enterText = (EditText) findViewById(R.id.enterText); 31 saveBtn = (Button) findViewById(R.id.saveBtn); 32 loadBtn = (Button) findViewById(R.id.loadBtn); 33 loadText = (TextView) findViewById(R.id.loadText); 34 35 saveBtn.setOnClickListener(new OnSaveListener()); 36 loadBtn.setOnClickListener(new OnLoadListener()); 37 }
按钮一调用服务器端save()方法:
1 private class OnSaveListener implements OnClickListener { 2 3 @Override 4 public void onClick(View v) {
// 三步之一,实例化SoapObject 5 SoapObject so = new SoapObject(NAMESPACE, SAVE_METHOD_NAME);
// 下面的参数是WebService端方法所需的参数 6 so.addProperty( "fileName", "shuai.txt" ); 7 so.addProperty( "content", enterText .getText().toString()); 8
// 三步之二, 实例化Envelope 9 SoapSerializationEnvelope sse = new SoapSerializationEnvelope(SoapEnvelope.VER11); 10 sse. bodyOut = so; 11 sse. dotNet = false; 12 sse.setOutputSoapObject(so); 13
// 三步之三, 实例化HttpTransportSE 14 HttpTransportSE htse = new HttpTransportSE(URL); 15 htse.debug = true; 16 try { 17 htse.call( SOAP_ACTION, sse); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } catch (XmlPullParserException e) { 21 e.printStackTrace(); 22 } 23 Toast. makeText(MainActivity.this, "成功保存" , Toast.LENGTH_LONG ).show(); 24 } 25 26 }
按钮二调用服务器端load()方法:
1 private class OnLoadListener implements OnClickListener { 2 3 @Override 4 public void onClick(View v) { 5 SoapObject so = new SoapObject(NAMESPACE, LOAD_METHOD_NAME); 6 so.addProperty( "fileName", "shuai.txt" ); 7 8 SoapSerializationEnvelope sse = new SoapSerializationEnvelope(SoapEnvelope.VER11); 9 sse. bodyOut = so; 10 sse. dotNet = false; 11 sse.setOutputSoapObject(so); 12 13 HttpTransportSE htse = new HttpTransportSE(URL); 14 htse. debug = true; 15 try { 16 htse.call( SOAP_ACTION, sse); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } catch (XmlPullParserException e) { 20 e.printStackTrace(); 21 }
// 获取web service返回数据 22 SoapObject result = (SoapObject) sse. bodyIn; 23 MainActivity. this.loadText .setText("返回数据为: " + result.getProperty(0)); 24 } 25 26 } 27 28 }