最近在忙一个android作业,准备的架构是C/S,服务器端用php,客户端在android上运行。打算用soap协议进行web service的交互,已经成功进php客户端和php服务器端的通信,所以服务器端应该没什么问题。准备移到andoid端。
网上查了一下,很多文章都提到ksoap2应该是android上很好的选择,但网上好的例子和文档真的很少,又用过的吗,可以给个清楚明白的链接或者直接贴段示例代码吗?
本人感激不尽!!
------解决方案--------------------
算了,我看了一下代码也不是很多,就贴出来算了。
记得自己去网上下载这个jar包:ksoap2-android-assembly-2.4-jar-with-dependencies.jar
- Java code
private static String NameSpace="http://tempuri.org/"; private static String u="http://10.8.8.70:808";//请更换成你要访问的服务器地址 private static String webService="/webService/WebService.asmx";//webService目录 private static String MethodName="HelloWorld2";//要调用的webService方法 private static String soapAction=NameSpace+MethodName; private static String url=u+webService; private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.tv); tv.setText(ws()); } //webService操作要访问网络,所以最好是使用线程来做,这里只是示例,所以就不考虑了 private String ws(){ String result=""; try{ SoapObject request=new SoapObject(NameSpace,MethodName);//NameSpace //webService方法中的参数,这个根据你的webservice来,可以没有。 //但请注意,参数名称和参数类型客户端和服务端一定要一致,否则将可能获取不到你想要的 //request.addProperty("x",5); //request.addProperty("y", 6); SoapSerializationEnvelope envelope=new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(request); HttpTransportSE ht=new HttpTransportSE(url); ht.call(soapAction, envelope); if(envelope.getResponse()!=null){ SoapPrimitive response=(SoapPrimitive)envelope.getResponse(); result=response.toString();//这里获得了webService的返回值 } }catch(Exception e){ result=e.getMessage(); } return result; }