STP1. 建立WebService, 預設Namespace, 方法名稱SNQuery, 輸入輸出皆為string
02 | using System.Collections.Generic; |
05 | using System.Web.Services; |
06 | using System.Data.SqlClient; |
08 | namespace SNQueryWebService |
14 | [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] |
15 | [System.ComponentModel.ToolboxItem( false )] |
18 | public class Service1 : System.Web.Services.WebService |
21 | public string HelloWorld() |
27 | public string SNQuery( string SN) |
29 | SqlConnection conn = new SqlConnection( "server=***;database=***;uid=***;pwd=***" ); |
31 | SqlCommand s_com = new SqlCommand(); |
32 | s_com.CommandText = "SELECT * FROM SerialNumber WHERE SN='" + SN + "'" ; |
33 | s_com.Connection = conn; |
34 | SqlDataReader s_read = s_com.ExecuteReader(); |
35 | bool result = s_read.HasRows; |
41 | return SN + " NOT Found" ; |
STP2. 部屬.asmx, .asmx.cs, bin至Server, IIS>AWS>ConvertToApplication, 加.asmx為預設畫面
STP3. 下載ksoap2, 我使用的版本是2.5.2, 複製至\libs下, 並匯入Eclipse, Project>Proerties>JavaBuildPath>Libraries>AddExternalJAR..>OK
STP4. File>New>AndroidApplicationProject, 編輯res/layout/activity_main.xml如下
STP5. 編輯src/../MainActivity.java如下
01 | package com.example.CallWebService; |
03 | import org.ksoap2.SoapEnvelope; |
04 | import org.ksoap2.serialization.SoapObject; |
05 | import org.ksoap2.serialization.SoapPrimitive; |
06 | import org.ksoap2.serialization.SoapSerializationEnvelope; |
07 | import org.ksoap2.transport.HttpTransportSE; |
09 | import android.os.Bundle; |
10 | import android.app.Activity; |
11 | import android.view.Menu; |
12 | import android.view.View; |
13 | import android.widget.EditText; |
14 | import android.widget.TextView; |
16 | public class MainActivity extends Activity { |
21 | private static final String METHOD_NAME = "SNQuery" ; |
23 | private EditText SNInput; |
24 | private TextView ResultOutput; |
27 | protected void onCreate(Bundle savedInstanceState) { |
28 | super .onCreate(savedInstanceState); |
29 | setContentView(R.layout.activity_main); |
30 | SNInput = (EditText) findViewById(R.id.SNEditText); |
31 | ResultOutput = (TextView) findViewById(R.id.ResultEditText); |
34 | public void SNonClick(View snView) { |
35 | Thread networkThread = new Thread() { |
39 | SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); |
40 | request.addProperty( "SN" , SNInput.getText().toString()); |
41 | SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); |
42 | envelope.dotNet = true ; |
43 | envelope.setOutputSoapObject(request); |
44 | HttpTransportSE ht = new HttpTransportSE(URL); |
45 | ht.call(SOAP_ACTION, envelope); |
46 | final SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); |
47 | runOnUiThread ( new Runnable(){ |
49 | ResultOutput.setText(response.toString()); |
52 | } catch (Exception e) { |
57 | networkThread.start(); |
61 | public boolean onCreateOptionsMenu(Menu menu) { |
63 | getMenuInflater().inflate(R.menu.main, menu); |
STP6. 於AndroidManifest.xml編輯網路存取連線
1 | < uses-permission android:name = "android.permission.INTERNET" /> |
STP7. 結果如下
REF. 原始檔