基本信息
源码名称:Android与.net交互例子源码下载(web service)
源码大小:0.45M
文件格式:.rar
开发语言:Java
更新时间:2015-03-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
web service实现 android 与.net 交互



import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.mytwitter.until.DialogUtil;
import com.mytwitter.acitivity.Main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends Activity {
 // 定义文本框接收值
 EditText etName, etPass;
 Button btnExit, btnLogin;
 TextView txtReg;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.login);
  etName = (EditText) findViewById(R.id.username_edit);
  etPass = (EditText) findViewById(R.id.password_edit);
  btnExit = (Button) findViewById(R.id.btn_Exit);
  btnLogin = (Button) findViewById(R.id.signin_button);
  txtReg = (TextView) findViewById(R.id.register_link);
  btnExit.setOnClickListener(new FinishListener(this)); // 绑定取消按钮监听事件
  btnLogin.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    if (validate()) {
     if (Checking()) {
      // 启动Main Activity
      Intent intent = new Intent(LoginActivity.this,
        Main.class);
      startActivity(intent);
      // 结束该Activity
      finish();
     }
    }
   }
  });
  // 注册链接监听器
  txtReg.setClickable(true);
  txtReg.setFocusable(true);
  txtReg.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // 启动注册Activity
    Intent intent = new Intent(LoginActivity.this, Register.class);
    startActivity(intent);
    finish();
   }
  });
 }
 // 对用户输入的用户名、密码进行校验
 private boolean validate() {
  String username = etName.getText().toString().trim();
  if (username.equals("")) {
   DialogUtil.showDialog(this, "用户账户是必填项!", false);
   return false;
  }
  String pwd = etPass.getText().toString().trim();
  if (pwd.equals("")) {
   DialogUtil.showDialog(this, "用户密码是必填项!", false);
   return false;
  }
  return true;
 }
 private boolean Checking() {
  String username = etName.getText().toString();
  String pwd = etPass.getText().toString();
  try {
   int n = CheckLogin(username, pwd);
   if (n <= 0) {
    DialogUtil.showDialog(this, "用户名或密码不对!", false);
    return false;
   }
  } catch (Exception e) {
   DialogUtil.showDialog(this, "服务器响应异常,请稍后再试!", false);
   return false;
  }
  return true;
 }
 // 测试方法
 public String SayHello() {
  String s = "";
  String serviceUrl = "http://192.168.1.100/WebService/WebServiceForAndroid.asmx?wsdl";
  String methodName = "HelloWorld";
  SoapObject request = new SoapObject("http://tempuri.org/", methodName);
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER10);
  envelope.dotNet = true;
  envelope.setOutputSoapObject(request);
  HttpTransportSE ht = new HttpTransportSE(serviceUrl);
  try {
   ht.call(null, envelope);
   if (envelope.getResponse() != null) {
    // SoapObject soapObject = (SoapObject) envelope.getResponse();
    Object object = (Object) envelope.getResponse();
    s = object.toString();
   } else {
    s = "error";
   }
  } catch (Exception e) {
   s = e.toString();
  }
  return s;
 }
 // 判断登录是否成功
 public int CheckLogin(String username, String pass) {
  int n = 0;
  String serviceUrl = "http://192.168.1.100/WebService/WebServiceForAndroid.asmx?wsdl";
  String methodName = "Verdict";
  SoapObject request = new SoapObject("http://tempuri.org/", methodName);
  request.addProperty("name", username);
  request.addProperty("pass", pass);
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER10);
  envelope.dotNet = true;
  envelope.setOutputSoapObject(request);
  HttpTransportSE ht = new HttpTransportSE(serviceUrl);
  try {
   ht.call(null, envelope);
   if (envelope.getResponse() != null) {
    Object object = (Object) envelope.getResponse();
    int m = Integer.parseInt(object.toString());
    if (m == 1) {
     n = 1;
    }
   }
  } catch (Exception e) {
  }
  return n;
 }