您的位置:首页 > 编程语言 > Java开发

死亡历险,Java做的安卓调用.net Webservice进行修改密码

2016-08-12 00:05 288 查看
public class DefaultActivity extends Activity implements OnClickListener{

    private ArrayList<String> resultList = new ArrayList<String>();

    private ArrayList<String> arrayList = new ArrayList<String>(); 
    private ArrayList<String> brrayList = new ArrayList<String>();

    private Thread thread;

    public String op = "";

    public String np = "";

    public String np2 = "";

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_default);

        

        //初始化View

        initView();

    }

private void initView() {

    btn_xgmm = (MyImageButton) findViewById(R.id.button_xgmm);

    btn_xgmm.setOnClickListener(this);
}

    public void onClick(View v) 

    {
switch (v.getId()) {

        case R.id.button_xgmm:

        inputTitleDialog();//弹出修改密码的窗口

            break;

        case R.id.button_tc:

        showDialog("结果","确认退出吗?");

        break;

        default:

            break;

        }

    }

@SuppressWarnings("deprecation")
private void inputTitleDialog() {
LayoutInflater factory = LayoutInflater.from(this);  

        final View textEntryView = factory.inflate(R.layout.dialog, null);

        final EditText editText_old = (EditText) textEntryView.findViewById(R.id.editText_old);  

        final EditText editText_new = (EditText)textEntryView.findViewById(R.id.editText_new);  

        final EditText editText_new2 = (EditText)textEntryView.findViewById(R.id.editText_new2);

        AlertDialog.Builder ad1 = new AlertDialog.Builder(DefaultActivity.this);       

        

        ad1.setTitle("修改密码:");  

        ad1.setView(textEntryView);

        

        ad1.setPositiveButton("确认", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int which) {

op = editText_old.getText().toString().trim();
       np = editText_new.getText().toString().trim();
       np2 = editText_new2.getText().toString().trim();

                        //运行线程来修改密码
thread = new Thread(new QueryThread());
                thread.start();
   }
});

        ad1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int which) {
   dialog.dismiss();
  }
 });

        ad1.show();// 显示对话框                   

    }

class QueryThread implements Runnable{

        @Override

        public void run() {

        checkPassword();

        }

    }

//修改密码
public void checkPassword() 

    {
HttpConnSoap soap = new HttpConnSoap();
arrayList.clear();    
 

    brrayList.clear();

    arrayList.add("username");

    arrayList.add("password_old");

    arrayList.add("password_new");

    brrayList.add("001");  

         brrayList.add(op);  

         brrayList.add(np);

         

    resultList = soap.GetWebServre("checkPassword", arrayList, brrayList);

    int kkk = resultList.size();

    if(kkk > 0)

    {
    String result=resultList.get(0);
     
    Message msg = new Message();
        msg.obj = result;
        handler.sendMessage(msg);

    }

    }

Handler handler = new Handler(){
@Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int result = Integer.parseInt(msg.obj.toString());
        switch(result){
        
        case 1:
         
       
Toast.makeText(DefaultActivity.this, "密码修改成功!", Toast.LENGTH_SHORT).show();
        break;
        case 0:
       
Toast.makeText(DefaultActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
        
        break;
        default:
        break;
        }
    }        

     };

private String filterHtml(String source) {  

        if(null == source){  

            return "";  

        }  

        return source.replaceAll("</?[^>]+>","").trim();  

    }

-----------------------------------------------------------

HttpConnSoap.java

package com.ui;  

  

import java.io.IOException;  

import java.io.InputStream;  

import java.io.OutputStream;  

import java.net.HttpURLConnection;  

import java.net.URL;  

import java.util.ArrayList;  

  

public class HttpConnSoap {  

    public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {  

        ArrayList<String> Values = new ArrayList<String>();  

          

        //ServerUrl是指webservice的url  

        //10.0.2.2是让android模拟器访问本地(PC)服务器,不能写成127.0.0.1  

        //11125是指端口号,即挂载到IIS上的时候开启的端口  

        //Service1.asmx是指提供服务的页面  

        String ServerUrl = "http://192.168.11.211:81/netWebService/WebService1.asmx";

        String soapAction = "http://tempuri.org/" + methodName;  

        //String data = "";  

        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"  

                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"  

                + "<soap:Body />";  

        String tps, vps, ts;  

        String mreakString = "";  

  

        mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";  

        for (int i = 0; i < Parameters.size(); i++) {  

            tps = Parameters.get(i).toString();  

            //设置该方法的参数为.net webService中的参数名称  

            vps = ParValues.get(i).toString();  

            ts = "<" + tps + ">" + vps + "</" + tps + ">";  

            mreakString = mreakString + ts;  

        }  

        mreakString = mreakString + "</" + methodName + ">";  

        

        String soap2 = "</soap:Envelope>";  

        String requestData = soap + mreakString + soap2;  

        //System.out.println(requestData);  

  

        try {  

            URL url = new URL(ServerUrl);  

            HttpURLConnection con = (HttpURLConnection) url.openConnection();  

            byte[] bytes = requestData.getBytes("utf-8");  

            con.setDoInput(true);  

            con.setDoOutput(true);  

            con.setUseCaches(false);  

            con.setConnectTimeout(6000);// 设置超时时间  

            con.setRequestMethod("POST");  

            con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");  

            con.setRequestProperty("SOAPAction", soapAction);  

            con.setRequestProperty("Content-Length", "" + bytes.length);  

            OutputStream outStream = con.getOutputStream();  

            outStream.write(bytes);  

            outStream.flush();  

            outStream.close();  

            InputStream inStream = con.getInputStream();  

  

            //data=parser(inStream);  

            //System.out.print("11");  

            Values = inputStreamtovaluelist(inStream, methodName);  

            //System.out.println(Values.size());  

            return Values;  

  

        } catch (Exception e) {  

            System.out.print("2221");  

            return null;  

        }  

    }  

  

    public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {  

        StringBuffer out = new StringBuffer();  

        String s1 = "";  

        byte[] b = new byte[4096];  

        ArrayList<String> Values = new ArrayList<String>();  

        Values.clear();  

  

        for (int n; (n = in.read(b)) != -1;) {  

            s1 = new String(b, 0, n);  

            out.append(s1);  

        }  

  

        System.out.println(out);  

        String[] s13 = s1.split("><");  

        String ifString = MonthsName + "Result";  

        String TS = "";  

        String vs = "";  

  

        Boolean getValueBoolean = false;  

        for (int i = 0; i < s13.length; i++) {  

            TS = s13[i];  

            System.out.println(TS);  

            int j, k, l;  

            j = TS.indexOf(ifString);  

            k = TS.lastIndexOf(ifString);  

  

            if (j >= 0) {  

                System.out.println(j);  

                if (getValueBoolean == false) {  

                    getValueBoolean = true;  

                } else {  

  

                }  

  

                if ((j >= 0) && (k > j)) {  

                    System.out.println("FFF" + TS.lastIndexOf("/" + ifString));  

                    //System.out.println(TS);  

                    l = ifString.length() + 1;  

                    vs = TS.substring(j + l, k - 2);  

                    //System.out.println("fff"+vs);  

                    Values.add(vs);  

                    System.out.println("退出" + vs);  

                    getValueBoolean = false;  

                    return Values;  

                }  

  

            }  

            if (TS.lastIndexOf("/" + ifString) >= 0) {  

                getValueBoolean = false;  

                return Values;  

            }  

            if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {  

                k = TS.length();  

                //System.out.println(TS);  

                vs = TS.substring(7, k - 8);  

                //System.out.println("f"+vs);  

                Values.add(vs);  

            }  

  

        }  

  

        return Values;  

    }  

  

}  

-----------------------------------------------------------

上.net代码

WebService1.asmx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using BLL;//要先在WebSite1上添加引用,引用BLL

//为什么using不了App_Code里面的类

//右击类文件,选择属性,有个"bulid action"生成操作,把"content"内容改成"Compile"编译.就ok 了

using skyiv;

using DAL;

namespace Android

{

    /// <summary>

    /// WebService1 的摘要说明

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.ToolboxItem(false)]

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

    // [System.Web.Script.Services.ScriptService]

    public class WebService1 : System.Web.Services.WebService

    {

        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World";

        }

        //必须加[WebMethod],不然运行http://localhost:81/netWebService/WebService1.asmx

        //(在IIS里项目上点右键,点浏览)后这个Add方法出不来 

        //如果端口有冲突,那么80改为81

        //打开IIS,在默认网站上按右键,点属性,把TCP端口80改成81即可

        //http://localhost:81/netWebService/WebService1.asmx?wsdl

        [WebMethod]

        public int Add(int a, int b)

        {

            return(a+b);

        }

        [WebMethod]

        public string login(string username, string password)

        {

            string result = "登录成功";

            string validatePwd = UserManager.GetUserPasswordByUserName(username);

            string qm = UserManager.GetnUserIDByUserName(username);

            //加密密码

            //string inputPwd = CryptogramManager.EncryptPassword(this.txtPassword.Text.Trim());

            des d = new des();

            string inputPwd = d.EncryStrHex(password);

            //通过对比密码,验证登录信息是否正确

            if (inputPwd.Trim() == validatePwd.Trim())

            {

                //Session["bh"] = username;

                //Session["qm"] = qm;

                result = "1";

            }

            else

            {

                result = "0";//"您输入的用户名或密码不正确!试试001账号吧。";

            }

            return result;

        }

        [WebMethod]

        public string checkPassword(string username, string password_old, string password_new)

        {

            string result = "密码验证成功";

            string validatePwd = UserManager.GetUserPasswordByUserName(username);

            des d = new des();

            string pwd = d.EncryStrHex(password_old.Trim());

            string pwd_new = d.EncryStrHex(password_new.Trim());

            //通过对比密码,验证登录信息是否正确

            if (pwd == validatePwd)

            {

                if (DAL.UserService.password_Edit(username, pwd_new))

                {

                    result = "1";

                }

                else

                {

                    result = "0";

                }

            }

            else

            {

                result = "0";//密码错误

            }

            return result;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息