第一次使用SOAP

1,首先下载导入jar包


权限

 <uses-permission android:name="android.permission.INTERNET" />  

2,

public class MainActivity extends Activity {

private EditText ed_userName;
private EditText ed_pwd;
private Button bt_logIn;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setView();

bt_logIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(){
private String result;


public void run() {
try {
String username = ed_userName.getText().toString();
   String pwd = ed_pwd.getText().toString();

//md5加密
   String mD5pwd = MD5Util.getStringMD5(pwd);

实例化SoapObject对象:SoapObject soapObject = new SoapObject(SERVICE_NAMESPACE, methodName); 第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。第二个参数表示要调用的WebService方法名。

       SoapObject request = new SoapObject("http://tempuri.org/", "GetUserInfo"); //两个参数分别是  空间命名,方法名,下图找到  空间命名和方法名

               


       String soapAction = "http://tempuri.org/" + "GetUserInfo";
    //传参
request.addProperty("username", username);  
request.addProperty("pwd", mD5pwd);  
//.使用SOAP1.1协议创建Envelop对象:设置SOAP协议的版本号,根据服务端WebService的版本号设置。
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  

     //设置bodyout属性 envelope.bodyOut = soapObject;
envelope.bodyOut = request;  

//支持.net
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
//.创建HttpTransportSE传输对象:HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); 
HttpTransportSE ht =  new HttpTransportSE("http://192.168.0.10:8018/webservice/WebService.asmx?op=GetUserInfo");  

调用webservice://
ht.call(soapAction, envelope);
获取服务器响应返回的SOAP消息:///
SoapObject object = (SoapObject) envelope.bodyIn;
       result = object.getProperty(0).toString();


       Log.d("zz","result="+ result);

       

} catch (Exception e) {
Log.i("zz","e="+e);
e.printStackTrace();
}
};
}.start();
}
});

private void setView() {
ed_userName = (EditText) findViewById(R.id.ed_username);
ed_pwd = (EditText) findViewById(R.id.ed_pwd);
bt_logIn = (Button) findViewById(R.id.bt_login);

}




3  md5   代码

public class MD5Util {

/**
* The jce MD5 message digest generator.
*/
private static MessageDigest md5;


public static final String encodeString(String string)
throws RuntimeException {
return byteToHex(digestString(string, null));
}

/**
* Retrieves a hexidecimal character sequence representing the MD5 digest of
* the specified character sequence, using the specified encoding to first
* convert the character sequence into a byte sequence. If the specified
* encoding is null, then ISO-8859-1 is assumed

* @param string
*            the string to encode.
* @param encoding
*            the encoding used to convert the string into the byte sequence
*            to submit for MD5 digest
* @return a hexidecimal character sequence representing the MD5 digest of
*         the specified string
* @throws HsqlUnsupportedOperationException
*             if an MD5 digest algorithm is not available through the
*             java.security.MessageDigest spi or the requested encoding is
*             not available
*/
public static final String encodeString(String string, String encoding)
throws RuntimeException {
return byteToHex(digestString(string, encoding));
}

/**
* Retrieves a byte sequence representing the MD5 digest of the specified
* character sequence, using the specified encoding to first convert the
* character sequence into a byte sequence. If the specified encoding is
* null, then ISO-8859-1 is assumed.

* @param string
*            the string to digest.
* @param encoding
*            the character encoding.
* @return the digest as an array of 16 bytes.
* @throws HsqlUnsupportedOperationException
*             if an MD5 digest algorithm is not available through the
*             java.security.MessageDigest spi or the requested encoding is
*             not available
*/
public static byte[] digestString(String string, String encoding)
throws RuntimeException {


byte[] data;


if (encoding == null) {
encoding = "ISO-8859-1";
}

try {
data = string.getBytes(encoding);
} catch (UnsupportedEncodingException x) {
throw new RuntimeException(x.toString());
}

return digestBytes(data);
}

/**
* Retrieves a byte sequence representing the MD5 digest of the specified
* byte sequence.

* @param data
*            the data to digest.
* @return the MD5 digest as an array of 16 bytes.
* @throws HsqlUnsupportedOperationException
*             if an MD5 digest algorithm is not available through the
*             java.security.MessageDigest spi
*/
public static final byte[] digestBytes(byte[] data) throws RuntimeException {


synchronized (MD5Util.class) {
if (md5 == null) {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.toString());
}
}


return md5.digest(data);
}
}
private static final char HEXCHAR[] = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static String byteToHex(byte b[]) {
int len = b.length;
char[] s = new char[len * 2];
for (int i = 0, j = 0; i < len; i++) {
int c = ((int) b[i]) & 0xff;
s[j++] = HEXCHAR[c >> 4 & 0xf];
s[j++] = HEXCHAR[c & 0xf];
}
return new String(s);
}
public static String getFileMd5(String filename) throws Exception {
return getFileMd5(filename, null);
}
public static String getFileMd5(String filename, String encoding)
throws Exception {
encoding = encoding == null ? "ISO-8859-1" : encoding;
File f = new File(filename);


if (!f.exists()) {
return "";
}
InputStream is = new FileInputStream(f);
byte[] buffer = new byte[1024];
MessageDigest digest = MessageDigest.getInstance("MD5");


int count;
while ((count = is.read(buffer)) > 0) {
digest.update(buffer, 0, count);
}
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String output = bigInt.toString(16);


is.close();
return output;
}

public static String getFileMd5(File file) throws Exception {
return getFileMd5(file, null);
}

public static String getFileMd5(File file, String encoding)
throws Exception {
encoding = encoding == null ? "ISO-8859-1" : encoding;
InputStream is = new FileInputStream(file);
byte[] buffer = new byte[1024];
MessageDigest digest = MessageDigest.getInstance("MD5");

int count;
while ((count = is.read(buffer)) > 0) {
digest.update(buffer, 0, count);
}
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String output = bigInt.toString(16);

is.close();
return output;
}

public static String getStringMD5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte b = md[i];
// System.out.println((int)b);
str[k++] = hexDigits[b >> 4 & 0xf];
str[k++] = hexDigits[b & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值