可能有人会遇到跟我一样的问题,我们Android都是用网络框架来进行一些请求,但是有的公司可能会要你使用webservice来玩,fuck!
此篇是基于eclipse的,使用as小伙伴需要寻找在项目的build.gradle文件中添加依赖的资源!(网上搜索应该有很多!)
调用webservice时候将相同的部分单独拎了出来作为共用方法,不用反复重写相同代码,具体如下:
/**
* 共用方法
* @param request
* @param methodName
* @return
*/
public final static String publicMethod(SoapObject request, String methodName, Context context){
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.dotNet = false;
envelope.setOutputSoapObject(request);
envelope.encodingStyle="UTF-8";
if(TieasyConfig.WEB_SERVICE_URI == null && context != null){
TieasyConfig.initializeConfig(context);//这步是初始化你的uri地址(公司一般有两个地址一个测试地址一个正式地址)
}
HttpTransportSE ht = new HttpTransportSE(TieasyConfig.WEB_SERVICE_URI);
try{
ht.call(TieasyConfig.WEB_SERVICE_NAMESPACE+methodName, envelope);
if(envelope.getResponse() != null){
Log.i("dog", "调用webservice结束");
return envelope.getResponse().toString();
} else {
Log.e("dog", "调用服务失败!");
}
}catch(Exception e){
if(e.getMessage() != null){
Log.e("dog", e.getMessage());
}else{
Log.e("dog", "webservice调用出现异常");
}
}
return null;
}
具体实例如下:
/**
* 发送公告通知
* @param username 发公告人
* @param toUsername 发给对象
* @param news 内容
* @return 0 添加失败 1添加成功 返回人名则提示该人名不存在
*/
public final static String sendBulletinInfo(String username, String toUsername, String title, String news){
SoapObject request = new SoapObject( TieasyConfig.WEB_SERVICE_NAMESPACE, TieasyConfig.WEB_SERVICE_SENDBULLETININFO);
request.addProperty("username",username);
request.addProperty("toUsername",toUsername);
request.addProperty("title",title);
request.addProperty("news",news);
String md5Code = StringUtil.encryptSha256(username+toUsername+title+news+TieasyConfig.WEB_SERVICE_KEY).toUpperCase();
request.addProperty("HashCode",md5Code);
String bak = publicMethod( request, TieasyConfig.WEB_SERVICE_SENDBULLETININFO,null);
if(bak != null && !"-1".equals(bak)){
return bak;
}
return null;
}
public static final String WEB_SERVICE_SENDBULLETININFO = "sendBulletinInfo";
public static final String WEB_SERVICE_NAMESPACE = "http://www.tyibs.com";(这随意填)
public static final String WEB_SERVICE_KEY = "XXXXXXXXXXXXX"; (这随意填不影响)是在TieasyConfig类中初始化好的常量;
bak是返回的json字符串,自行解析即可