主要內容是webservice()設定,其他部分則是測試內容
(二)main.java
package nfu.edu; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class TestActivity extends Activity { /** Called when the activity is first created. */ private EditText input; private TextView output; private Button btn; private static Handler handler;//執行緒的處理事件 private static Runnable right; private static Runnable error; private static String result; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); input=(EditText)findViewById(R.id.editText);//讀取值的位置 output=(TextView)findViewById(R.id.textView1); btn=(Button)findViewById(R.id.btn); btn.setOnClickListener(new Button.OnClickListener(){ //按下按鈕的事件 public void onClick(View v) { // TODO Auto-generated method stub //進度視窗的提示(執行緒) final ProgressDialog mydialog=new ProgressDialog(TestActivity.this);//檔案名稱 mydialog.setTitle("請稍後");//視窗標題 mydialog.setMessage("執行中");//顯示訊息 mydialog.show();//指令顯示 System.out.println("1");//執行緒的流程 new Thread(){//宣告執行緒 public void run(){ int num; int x = 10; int y = 2;//0會進入例外事件 try{//正常處理 //num = x/y; //模擬伺服器處理結果(但會出現錯誤訊息,故等執行緒完成後再另外處理) System.out.println("2: + num"); result = WebService.Welcome();//呼叫107行webservice.welcome() sleep(3000);//延遲時間 handler.post(right);//標題檔.處理區塊 //output.setText(String.valueOf(num));//數值轉字串 }catch(Exception a){//例外處理 System.out.println("3"); handler.post(error); a.printStackTrace(); }finally{//指令移除 System.out.println("4"); mydialog.dismiss(); } } }.start(); //處理事件 handler = new Handler(); right = new Runnable(){ public void run() { // TODO Auto-generated method stub System.out.println("6"); output.setText(String.valueOf(result)); } }; handler = new Handler(); error = new Runnable(){ public void run() { // TODO Auto-generated method stub System.out.println("7"); output.setText("執行錯誤"); } }; System.out.println("5"); //output.setText("hey"); //output.setText(input.getText().toString());//將輸入值轉為字串輸出 } }); } //-----webservice start public static class WebService { //service連線設定 public static final String NAMESPACE = "http://tempuri.org/"; //以asp環境 public static final String URL_PATH = "http://10.0.2.2/MSG/"; //asp專案命名/MSG public static final String SERVICE_NAME = "WebService.asmx"; public static String Welcome(){ String method = "HelloWorld";//呼叫server的HelloWorld() String output = ""; try { SoapObject request = new SoapObject(NAMESPACE, method); //request.addProperty("whoName", personal); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_PATH + SERVICE_NAME); androidHttpTransport.call(NAMESPACE + method, envelope); Object result = envelope.getResponse(); output = result.toString(); }catch(Exception e){} System.gc(); return output; } } //-----webservice end }
(三)AndroidManifest.xml網路連線設定
(四)ASP環境,以c#為例
加入新項目(Ctrl+Shift+A)>WebService>
手機端呼叫的webservice的helloworld()。
(五)匯入kaoap設定