您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何通過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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。