死亡歷險,Java做的安卓調用.net Webservice進行修改密碼

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;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章