本篇文章为大家展示了如何通过ksoap2调用webservice,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
package com.study.ws; import java.io.IOException; import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebSettings; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class WSClient extends Activity { private Button search; private EditText phone; private TextView result; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); search=(Button)findViewById(R.id.search); phone=(EditText)findViewById(R.id.phone); result=(TextView)findViewById(R.id.result); search.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String strPhone=phone.getText().toString().trim(); if(!(strPhone.length()==0||strPhone.equals(""))) { String str = ""; try { str = getPhoneInfo(strPhone); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } result.setText(str); } } }); } public String getPhoneInfo(String phoneName) throws IOException, XmlPullParserException { //返回的查询结果 String result = null; //调用webservice接口的名称空间 String nameSpace="http://WebXml.com.cn/"; //调用的方法名 String methodName="getMobileCodeInfo"; //将方法名和名称空间绑定在一起 String SOAP_ACTION = nameSpace + methodName; //获得返回请求对象 SoapObject request = new SoapObject(nameSpace, methodName); //设置需要返回请求对象的参数 request.addProperty("mobileCode", phoneName); request.addProperty("userId", ""); //设置soap的版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); //设置是否调用的是dotNet开发的 envelope.dotNet = true; // envelope.bodyOut = request; AndroidHttpTransport hts = new AndroidHttpTransport ("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); // web service请求 hts.call(SOAP_ACTION, envelope); // 得到返回结果 Object o = envelope.getResponse(); result = o.toString(); return result; } }
上述内容就是如何通过ksoap2调用webservice,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。