Android 調用.NET webservice

來源:互聯網
上載者:User
package com.fairyeye.simple;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;public class MiniSystemActivity extends Activity {    /** Called when the activity is first created. */    private EditText userName;    private EditText userPass;    private Button btnLogin;    private Button btnClose;    private TextView tv;    final String SOAP_ACTION = "http://tempuri.org/HelloWorld";     private static final String METHOD_NAME = "HelloWorld";     private static final String NAMESPACE = "http://tempuri.org/";     private static final String URL = "http://10.0.2.2/webser/webtest.asmx";     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btnLogin = (Button) findViewById(R.id.btnLogin);        btnClose = (Button) findViewById(R.id.btnClose);        userName = (EditText) findViewById(R.id.userName);        userPass = (EditText) findViewById(R.id.userPass);        tv = (TextView)findViewById(R.id.txtView);        btnLogin.setOnClickListener(listener);        call();    }    private OnClickListener listener = new OnClickListener() {        public void onClick(View v) {            // TODO Auto-generated method stub            if (userName.getText().toString().trim().equals("222")                    && userPass.getText().toString().trim().equals("222")) {                Intent intent = new Intent();                intent.setClass(MiniSystemActivity.this, testActivity.class);                startActivity(intent);            } else {                Toast.makeText(MiniSystemActivity.this, "使用者或密碼錯誤!",                        Toast.LENGTH_LONG).show();            }        }    };    public void call() {        try {            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);            //request.addProperty("passonString", "Rajapandian"); //這個是傳遞參數的            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(                    SoapEnvelope.VER11);            envelope.dotNet = true;            envelope.setOutputSoapObject(request);            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);            androidHttpTransport.call(SOAP_ACTION, envelope);            Object result = (Object) envelope.getResponse();            tv.setText(result.toString());         } catch (Exception e) {            System.out.println(e.getMessage());            tv.setText(e.getMessage());        }    }}

---------------------------------以上 實現了 Android內通過Ksoap包的方法 調用了 .net webserivce中 helloworld 方法。

 

1.在ADT 虛擬機器中 localhost/127.0.01是不行的。虛擬機器中預設10.0.2.2為本地地址 所以我們在本地的.NET webservice 如果為localhost/webser/webtest.asmx 在Android模擬器中為10.0.2.2/webser/webtest/asmx.(該問題 親自測試)

2.記得 在AndroidManifest.xml 加入 允許訪問 Internet  <uses-permission android:name="android.permission.INTERNET" />

 

轉:http://blog.csdn.net/fairyeye/article/details/6672466

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.