友链:汪菜菜

友链:隔壁老王

webservice接口的调用(登录)

                //自定义login方法 里面需要传入账户与密码
                public String login(String login_name, String password) {

                    //WebServices地址
                    String WebServicesURL = "http://192.168.2.113/CatlServer.asmx";
                    //命名空间
                    String NameSpace = "http://tempuri.org/";
                    //接口方法名
                    String MethodName = "Login";
                    //SOAP协议定义消息请求的地址
                    String soapAction = "http://tempuri.org/Login";

                    //实例化SoapObject 对象
                    SoapObject request = new SoapObject(NameSpace, MethodName);
                    //调用WebService接口传入两个参数
                    request.addProperty("loginName", login_name);
                    request.addProperty("password", password);

                    //生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
                    //设置bodyOut,发送请求
                    envelope.bodyOut = request;
                    // 设置是否调用的是dotNet开发的WebService,后续注释掉或许能解决ksoap2参数传输不了或者失败
                    envelope.dotNet = true;
                    //网络请求
                    HttpTransportSE httpTransportSE = new HttpTransportSE(WebServicesURL);
                    try {
                        httpTransportSE.call(soapAction, envelope);//这里需要传入一下两个参数
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //获取返回的数据
                    SoapObject object = (SoapObject) envelope.bodyIn;
                    //获取返回的结果,后续根据返回值做判断
                    String result = object.getProperty(0).toString();

                    //逻辑判断 状态码为1则成功跳转到主界面
                    if (mEditText_username.length() < 1) {
                        Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_LONG).show();
                    } else if (mEditText_password.length() < 1) {
                        Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    } else if(result.equals("200")){
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        Toast.makeText(LoginActivity.this, "登录成功,欢迎您\t" + str_username, Toast.LENGTH_LONG).show();
                        startActivity(intent);
                    }
                    else if (result.equals("0")) {
                        Log.e("tag", "密码错误");
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                    } else if (result.equals("1")) {
                        Log.e("tag", "成功");
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        Toast.makeText(LoginActivity.this, "登录成功,欢迎您\t" + str_username, Toast.LENGTH_LONG).show();
                        startActivity(intent);
                    } else if (result.equals("2")) {
                        Log.e("tag", "用户名错误");
                        Toast.makeText(LoginActivity.this, "用户名错误", Toast.LENGTH_LONG).show();
                    } else if (result.equals("-1")) {
                        Log.e("tag", "未找到用户信息");
                        Toast.makeText(LoginActivity.this, "未找到用户信息", Toast.LENGTH_LONG).show();
                    }
                    return result;
                }

 

posted @ 2022-06-08 10:37  長安憶ღ  阅读(303)  评论(0)    收藏  举报