80,350
社区成员
发帖
与我相关
我的任务
分享
String namespace ="http://tempuri.org/";
String endpoint = "http://116.55.248.28:9999/UserInfoService.asmx";
String method = "CheckUserLoginIsExit";
String SOAPAction = "http://tempuri.org/CheckUserLoginIsExit";
SoapObject request = new SoapObject(namespace, method);
request.addProperty("UserLogin", "muou");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.encodingStyle="UTF-8";
envelope.bodyOut=request;
envelope.dotNet=true;
//envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(endpoint);
try {
androidHttpTransport.call(SOAPAction, envelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//SoapObject object = null;
SoapObject object = (SoapObject )envelope.bodyIn;
if(object==null){
Toast.makeText(getApplicationContext(), "object is null", Toast.LENGTH_LONG).show();
}
public class TestWebService extends Activity {
private static String NameSpace="http://tempuri.org/";//这个不要改
//http://116.55.248.28:9999/UserInfoService.asmx
private static String u="http://116.55.248.28:9999";//请更换成你要访问的服务器地址
private static String webService="/UserInfoService.asmx";//webService目录
private static String MethodName="CheckUserLoginIsExit";//要调用的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("UserLogin", "muou");
//request.addProperty("y", 6);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet=true;//访问.NET的webservice
envelope.setOutputSoapObject(request);
HttpTransportSE ht=new HttpTransportSE(url);
ht.call(soapAction, envelope);//调用call方法,访问webservice
if(envelope.getResponse()!=null){
SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
//如果要返回对象集合,在服务端可以将对象或集合序列化成json字符串返回,这边再反序列化成对象或集合
result=response.toString();//这里获得了webService的返回值
}
}catch(Exception e){
result=e.getMessage();
System.out.println("e="+result);
}
return result;
}
}