死亡历险,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;
}
}
}
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;
}
}
}
相关文章推荐
- 死亡历险,asp.net做的Webservice,Java做的安卓调用Webservice进行登录验证
- 死亡历险,翻开新的一篇之Delphi 连接 JAVA WebService,并实现查找,添加,修改,删除等功能
- 安卓学习笔记---JNI实现Java与C之间进行互相调用
- Weblogic:Java调用Weblogic API创建用户和修改密码
- Java 使用SSH连接Linux 使用passwd命令修改密码
- JavaAdapter扩展进行Service调用拦截处理
- 调用存储过程进行图片的插入或修改
- 应用JAVA进行密码加密的一种算法
- JSP调用sql server数据库的内容对用户密码进行验证
- 写一个方法进行各种属性的更新,而不要每个界面设置改变都改变一个属性。就是点击应用或确定按钮时调用这个更新属性的方法,遍历所有(控制对象属性的)界面控件的状态(值),进行属性修改和刷新。
- 基础--JAVA对Mysql进行连接、插入、修改、删除操作
- Java 反射进行动态方法调用
- 使用ASM对JAVA class file进行修改的技术 -- 添加类成员
- Java调用T-SQL进行数据库的备份和还原
- java中通过jacob调用dts进行数据导入导出
- 上一篇,在Websphere上使用Axis做的客户端调用Webservice要进行修改配置的原因
- java开发技巧:对jar包进行再次修改
- 直接调用ASPNETDB数据库中(aspnet_Membership_SetPassword)存储过程来修改 用户密码
- oracle如何对用户进行解锁、修改密码