android 连接 asp.net webservice 简单记录

之前一直也没有做过android的项目  java也是几年前学过  早忘记了  

但是用到的时候还是得学学  所以对于android 就更加的是一个初学者了

只是针对于遇到的几个问题做个解释

环境是这样的:

Eclipse: 最新的最好

JDK: 1.7

Android SDK: 22.62

KSoap2:    ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar   ,   ksoap2-android.jar

至于为什么贴出来两个KSoap2的jar包 其实呢 我也只是遇到一个问题 需要两个  后续会说明

创建自己的asp.net的 webservice

我们可以看到 在4.0的时候 是没有asp.net web 服务 这个项目类型的  其实4.0也是可以建立的 只不过要建立一个asp.net的空网站 但是在这里就不说了
我们直接选择4.0以下的版本 是有这个的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using TestClass;

[WebService(Namespace = "http://lizq.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string LoginUser(UserModel model)
    {
        return new LoginService().LoginUser(model);
    }
    
}


对于 传入的参数 UserMode  是我们自己建立的一个对象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestClass
{
    public class UserModel
    {
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value;}
        }

        private string passWord;

        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; }
        }
    }
}


最后就是把webservice 部署到IIS

我们来查看一下部署后的信息

 

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button loginButton=(Button)findViewById(R.id.button1);
        nameText=(EditText)findViewById(R.id.editText1);
        passText=(EditText)findViewById(R.id.editText2);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads()
        .detectDiskWrites()
        .detectNetwork()   // or .detectAll() for all detectable problems
        .penaltyLog()
        .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectLeakedSqlLiteObjects()
        .detectLeakedClosableObjects()
        .penaltyLog()
        .penaltyDeath()
        .build());
        loginButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Login();
            }
            
        });
        
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
            
        }
    }

 

 protected void Login(){
    
        String nameSpace = "http://lizq.org/";
          String methodName = "LoginUser";
          String soapAction = "http://lizq.org/LoginUser";

          String url = "http://192.168.1.42:5179/Service.asmx?WSDL";//后面加不加那个?wsdl参数影响都不大
          
          //建立webservice连接对象
          org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url,30000);
          transport.debug = true;//是否是调试模式
          
          //设置连接参数
          SoapObject soapObject = new SoapObject(nameSpace, methodName);

        
        UserModel user=new UserModel();
        user.setProperty(0, nameText.getText().toString());
          user.setProperty(1, passText.getText().toString());
          
        PropertyInfo pi = new PropertyInfo();
        pi.setName("model");
        pi.setValue(user);
        pi.setType(user.getClass());
        soapObject.addProperty(pi);//将自定参数加入请求对象中
        
        //设置返回参数
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);//soap协议版本必须用SoapEnvelope.VER11(Soap V1.1)
        envelope.dotNet = true;//注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 不指定rpc方式则用true否则要用false
        envelope.bodyOut = soapObject;
       //envelope.setOutputSoapObject(soapObject);//设置请求参数
        envelope.addMapping(nameSpace, "UserModel", user.getClass());//传对象时必须,参数namespace是webservice中指定的, name是服务器类型的名称, claszz是自定义类的类型
        
        try {
             transport.call(soapAction, envelope);
             Object sb = (Object)envelope.bodyIn;//服务器返回的对象存在envelope的bodyIn中
             Object us= (Object)envelope.getResponse();//直接将返回值强制转换为已知对象
             
             if(us.toString()==""){
             
             }
             
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } catch (XmlPullParserException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } catch(Exception ex){
         ex.printStackTrace();
        }
          
        return ;
    }

 

由于时间问题  不写了  

 

遇到问题 去这几个网站

http://blog.csdn.net/xiaochunyong/article/details/7765338

http://blog.csdn.net/java_chuan/article/details/6617776

http://blog.csdn.net/zjtbetter/article/details/12890831

http://my.eoe.cn/iceskysl/archive/4382.html

 

 

posted @ 2014-03-28 10:14  lizhiqiang1191  阅读(323)  评论(0编辑  收藏  举报