Android调用webservice 并传递实体类.docx_第1页
Android调用webservice 并传递实体类.docx_第2页
Android调用webservice 并传递实体类.docx_第3页
Android调用webservice 并传递实体类.docx_第4页
Android调用webservice 并传递实体类.docx_第5页
免费预览已结束,剩余6页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本来以为在java平台上用axis2生成了客户端代理类然后移植到Android平台上就好了。没想到在移植过程中出现了很多问题。说明JVM和android的DVM差距还是很大的。JVM执行的是class文件,而DVM执行的是dex文件。在eclipse里面开发Android程序的时候在编译时会把jar包里面的class一个个编译成DVM可执行的dex文件。当然,有个前提是jar包是放在source folder里面的。这样eclipse才会在编译程序的时候将jar包编译到apk文件中去。要不然虽然本地eclipse不会报错,但是在模拟器中会报错NoClassDefFound。而且有的jar包是不能被dexdump.exe正确转换成dex文件的。这样就导致这个jar包不能用,后果是整个程序都不能正确运行。我在将axis2移植到Android平台上去的时候有一些jar包转换不了。然后网上找了很多资料,都没人解决这个问题。希望如果有人解决了能共享一下下。后来实在不行了,看网上说在Android平台都用ksoap2来调用Web Service。自己觉得解决不了axis2的问题。于是只能改变方向。学习了一下ksoap2。在ksoap2调用WCF服务的时候也出现了很多问题。好在后来慢慢都解决了。现在将我遇到的问题和解决的方案都写下来,供其他也碰到这些问题的人参考。下面列举一下我碰到的问题和解决方案1.调用是参数的说明view plaincopy to clipboardprint?01.static String NameSpace=/; 02.static String URL=:9001/test; 03.static String SOAP_ACTION=/ITestService/GetUser; 04.static String MethodName=GetUser; static String NameSpace=/; static String URL=:9001/test; static String SOAP_ACTION=/ITestService/GetUser; static String MethodName=GetUser; Namespace 是你设置的服务命名空间,一般没有设置就是/URL是你服务暴露的地址,通过这个地址可以获取wsdl。在android里面代表的是模拟器的地址,而代表的才是电脑的。所以如果是自己本机做WCF服务器的话,程序里面应该这么设置。SOAP_ACTION是你的wsdl里面相对应的方法的地址。MethodName就是SOAP_ACTION最后面的那个指明ACTION的方法名。2.参数传递 复杂对象服务里面不可避免的是会传递参数,但是在可能在wcf服务端可能解析不了你传的参数。通过tcptrace截取soap后发现是参数的namespace不对应的原因。下面是一个例子服务端代码:view plaincopy to clipboardprint?01.User ITestService.GetUser(User u) 02. 03. User user = new User(); 04. user.UId = Server: + u.UId; 05. user.UName = Server: + u.UName; 06. return user; 07. User ITestService.GetUser(User u) User user = new User(); user.UId = Server: + u.UId; user.UName = Server: + u.UName; return user; User类:view plaincopy to clipboardprint?01.DataContract 02. public class User 03. 04. DataMember 05. public string UId; 06. DataMember 07. public string UName; 08. DataContract public class User DataMember public string UId; DataMember public string UName; android客户端代码如下:view plaincopy to clipboardprint?01.SoapObject requet=new SoapObject(NameSpace,MethodName); 02. 03. PropertyInfo perPropertyInfo=new PropertyInfo(); 04. User user=new User(); 05. user.setUId(123); 06. user.setUName(cch); 07. perPropertyInfo.setName(u); 08. perPropertyInfo.setValue(user); 09. perPropertyInfo.setType(User.class); 10. requet.addProperty(perPropertyInfo); 11. 12. SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); 13. envelope.addMapping(User.NAMESPACE,User,User.class);/register 这个很重要 14. envelope.setOutputSoapObject(requet); 15. envelope.dotNet=true; 16. AndroidHttpTransport transport=new AndroidHttpTransport (URL); 17. 18. ClientUtil.SetCertification(); /设置证书 19. try 20. 21. transport.call(SOAP_ACTION,envelope); 22./ 23. SoapObject response=(SoapObject)envelope.getResponse(); 24./ 25. /PraseXML_SF(response); 26. (TextView)findViewById(R.id.txt01).setText(String.valueOf(response.toString(); 27. catch (IOException e) 28. / TODO Auto-generated catch block 29. e.printStackTrace(); 30. catch (XmlPullParserException e) 31. / TODO Auto-generated catch block 32. e.printStackTrace(); 33. SoapObject requet=new SoapObject(NameSpace,MethodName); PropertyInfo perPropertyInfo=new PropertyInfo(); User user=new User(); user.setUId(123); user.setUName(cch); perPropertyInfo.setName(u); perPropertyInfo.setValue(user); perPropertyInfo.setType(User.class); requet.addProperty(perPropertyInfo); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.addMapping(User.NAMESPACE,User,User.class);/register 这个很重要 envelope.setOutputSoapObject(requet); envelope.dotNet=true; AndroidHttpTransport transport=new AndroidHttpTransport (URL); ClientUtil.SetCertification(); /设置证书 try transport.call(SOAP_ACTION,envelope);/SoapObject response=(SoapObject)envelope.getResponse();/PraseXML_SF(response);(TextView)findViewById(R.id.txt01).setText(String.valueOf(response.toString(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (XmlPullParserException e) / TODO Auto-generated catch blocke.printStackTrace(); android端也有一个User类,这个类是继承的BaseObject,BaseObject实现KvmSerializable接口先BaseObject:view plaincopy to clipboardprint?01.package CCH.Model; 02.import org.ksoap2.serialization.KvmSerializable; 03.import org.ksoap2.serialization.SoapObject; 04.public abstract class BaseObject implements KvmSerializable 05. 06. public static final String NAMESPACE = /2004/07/TestService; 07. /public static final String NAMESPACE = /2004/07/HL7.Base.Struct; 08. public BaseObject() 09. super(); 10. 11. 12. package CCH.Model;import org.ksoap2.serialization.KvmSerializable;import org.ksoap2.serialization.SoapObject;public abstract class BaseObject implements KvmSerializablepublic static final String NAMESPACE = /2004/07/TestService;/public static final String NAMESPACE = /2004/07/HL7.Base.Struct; public BaseObject() super(); 然后是User类view plaincopy to clipboardprint?01.package CCH.Model; 02.import java.util.Hashtable; 03.import org.ksoap2.serialization.PropertyInfo; 04.public class User extends BaseObject 05. 06. private String UId; 07. private String UName; 08. 09. public Object getProperty(int index) 10. / TODO Auto-generated method stub 11. switch (index) 12. case 0: 13. return UId; 14. case 1: 15. return UName; 16. default: 17. return null; 18. 19. 20. public int getPropertyCount() 21. / TODO Auto-generated method stub 22. return 2; 23. 24. public void getPropertyInfo(int index, Hashtable ht, PropertyInfo info) 25. / TODO Auto-generated method stub 26. space=super.NAMESPACE;/这个很重要 27. switch (index) 28. case 0: 29. info.type=PropertyInfo.STRING_CLASS; 30. =UId; 31. break; 32. case 1: 33. info.type=PropertyInfo.STRING_CLASS; 34. =UName; 35. break; 36. default: 37. break; 38. 39. 40. public void setProperty(int index, Object value) 41. / TODO Auto-generated method stub 42. switch (index) 43. case 0: 44. UId=value.toString(); 45. break; 46. case 1: 47. UName=value.toString(); 48. break; 49. default: 50. break; 51. 52. 53. public String getUId() 54. return UId; 55. 56. public void setUId(String uId) 57. UId = uId; 58. 59. public String getUName() 60. return UName; 61. 62. public void setUName(String uName) 63. UName = uName; 64. 65. 66. package CCH.Model;import java.util.Hashtable;import org.ksoap2.serialization.PropertyInfo;public class User extends BaseObjectprivate String UId;private String UName;public Object getProperty(int index) / TODO Auto-generated method stubswitch (index) case 0:return UId;case 1:return UName;default:return null;public int getPropertyCount() / TODO Auto-generated method stubreturn 2;public void getPropertyInfo(int index, Hashtable ht, PropertyInfo info) / TODO Auto-generated method space=super.NAMESPACE;/这个很重要switch (index) case 0:info.type=PropertyInfo.STRING_CLASS;=UId;break;case 1:info.type=PropertyInfo.STRING_CLASS;=UName;break;default:break;public void setProperty(int index, Object value) / TODO Auto-generated method stubswitch (index) case 0:UId=value.toString();break;case 1:UName=value.toString();break;default:break;public String getUId() return UId;public void setUId(String uId) UId = uId;public String getUName() return UName;public void setUName(String uName) UName = uName; 因为要序列化啊什么什么的,解释起来比较烦。这边也不解释了。大家有兴趣可以去查一下。只说明一下是通过space+来反序列化的。3.如果有证书加密,会一直说timeout。解决方法是在android客户端调用下面这个方法。这个方法要在httptransport.call()之前调用view plaincopy to clipboardprint?01.ClientUtil.SetCertification(); /设置证书 ClientUtil.SetCertification(); /设置证书类是这么写的:view plaincopy to clipboardprint?01.public class ClientUtil 02. /设置证书被信任 03. public static void SetCertification() 04. try 05. HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() 06. Override 07. public boolean verify(String hostname, 08. SSLSession session) 09. / TODO Auto-generated method stub 10. return true; 11. ); 12. SSLContext context = SSLContext.getInstance(TLS); 13. context.init(null, new X509TrustManagernew X509TrustManager() 14. public void checkClientTrusted(X509Certificate chain, 15. String authType) throws CertificateException 16. public void checkServerTrusted(X509Certificate chain, 17. String authType) throws CertificateException 18. public X509Certificate getAcceptedIssuers() 19. return new X509Certificate0; 20. , new SecureRandom(); 21. HttpsURLConnection.setDefaultSSLSocketFactory( 22. context.getSocketFactory(); 23. catch (Exception e) 24. e.printStackTrace(); 25. 26. 27. public class ClientUtil /设置证书被信任public static void SetCertification() try HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()Overridepublic boolean verify(String hostname,SSLSession session) / TODO Auto-generated method stubreturn true;); SSLContext context = SSLContext.getInstance(TLS); context.init(null, new X509TrustManagernew X509TrustManager() public void checkClientTrusted(X509Certificate chain, String authType) throws CertificateException public void checkServerTrusted(X509Certificate chain, String authType) throws CertificateException public X509Certificate getAcceptedIssuers() return new X509Certificate0; , new SecureRandom(); HttpsURLConnection.setDefaultSSLSocketFactory( context.getSocketFactory(); catch (Exception e) e.printStackTrace(); 这个信任一切证书。应为自制的证书是不被信任的,所以shakehand的时候一直timeout。4.wcf设置的自定义帐号密码认证(userNameAuthentication )加入这个之后要在soap发送前在head里面加入用户信息。加入的方法是:view plaincopy to clipboardprint?01./* 02. * authenticator 加入密码账号验证 03. * */ 04. ServiceConnection connection=super.getServiceConnection(); 05. String Login=Base64.encode(cch:cch1.getBytes(); 06. connection.setRequestProperty(Authorization,Basic +Login); /* * authenticator 加入密码账号验证 * */ServiceConnection connection=super.getServiceConnection();String Login=Base64.encode(cch:cch1.getBytes();connection.setRequestProperty(Authorization,Basic +Login); 这个需要重写httptransport的getServiceConnection()方法。因为在调用httptransport.call()的时候Connection才被初始化,所以在程序外getServiceConnection().setRequestProperty()会报错说nullpoi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论