Android调用WebService服务

来源:互联网 发布:java负数转正数 编辑:程序博客网 时间:2024/05/21 14:06


转载请标明出处:http://blog.csdn.net/u013598111/article/details/50057161,本文出自:【JunTao_sun】

下载Ksoap2-android-assembly-3.0.0-jar 


下面是查询 广东省得到的城市结果:

<span style="font-size:18px;">This XML file does not appear to have any style information associated with it. The document tree is shown below.<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/"><string>广州 (59287)</string><string>深圳 (59493)</string><string>潮州 (59312)</string><string>韶关 (59082)</string><string>湛江 (59658)</string><string>惠州 (59298)</string><string>清远 (59280)</string><string>东莞 (59289)</string><string>江门 (59473)</string><string>茂名 (59659)</string><string>肇庆 (59278)</string><string>汕尾 (59501)</string><string>河源 (59293)</string><string>揭阳 (59315)</string><string>梅州 (59117)</string><string>中山 (59485)</string><string>德庆 (59269)</string><string>阳江 (59663)</string><string>云浮 (59471)</string><string>珠海 (59488)</string><string>汕头 (59316)</string><string>佛山 (59279)</string></ArrayOfString></span>
SoapObject object = (SoapObject) envelope.bodyIn的结果是字符串

SoapObject detail = (SoapObject) envelope.getResponse();得到的是一维                                                                    字符串数组再解析


<span style="font-size:18px;">package com.example.wwwww;import java.io.IOException;import java.io.UnsupportedEncodingException;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.MarshalBase64;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {// 命名空间private static final String NAMESPACE = "http://WebXml.com.cn/";// WebService地址private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";// +?wsdl// 调用的方法名称private static final String METHOD_NAME = "getSupportCity ";private static final String TAG = null;// SOAP Actionprivate static String action = "http://WebXml.com.cn/getSupportCity ";private String weatherToday;private TextView text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button) findViewById(R.id.btn);text = (TextView) findViewById(R.id.text);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//网络请求操作要另开启线程new Thread(new Runnable() {@Overridepublic void run() {//1查询所在省 的城市结果getCity();// 2  查询手机号码// getRemoteInfo();}}).start();}});}public void getRemoteInfo() {// 命名空间String nameSpace = "http://WebXml.com.cn/";// 调用的方法名称String methodName = "getMobileCodeInfo";// webservice 地址String webservicePoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";// SOAP ActionString soapAction = "http://WebXml.com.cn/getMobileCodeInfo";// 第一 指定WebService的命名空间和调用的方法名SoapObject rpc = new SoapObject(nameSpace, methodName);// 第二 设置需调用WebService接口需要传入的两个参数mobileCode、userIdrpc.addProperty("mobileCode", "1375025");rpc.addProperty("userId", "");// 第三 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// envelope.bodyOut = rpc;// 设置是否调用的是dotNet开发的WebServiceenvelope.dotNet = true;// 等价于envelope.bodyOut = rpc;envelope.setOutputSoapObject(rpc);// 第四 用Http传输包装信息 发起请求HttpTransportSE transport = new HttpTransportSE(webservicePoint);try {// 调用WebServicetransport.call(soapAction, envelope);} catch (Exception e) {e.printStackTrace();}//第五   获取返回的数据SoapObject object = (SoapObject) envelope.bodyIn;// 获取返回的结果if (object != null) {String result = object.getProperty(0).toString();handler.obtainMessage(0x22, result).sendToTarget();}}private void getCity() {SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);rpc.addProperty("byProvinceName", "广东");SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = rpc;envelope.dotNet = true;envelope.setOutputSoapObject(rpc);HttpTransportSE ht = new HttpTransportSE(URL);ht.debug = true;try {ht.call(action, envelope);// 得到一维字符数组SoapObject detail = (SoapObject) envelope.getResponse();// 得到字符串SoapObject detail2 = (SoapObject) envelope.bodyIn;// if (detail != null)String result = detail.getProperty(0).toString();handler.obtainMessage(0x22, result).sendToTarget();Log.e(TAG, detail.toString());} catch (IOException e) {e.printStackTrace();} catch (XmlPullParserException e) {e.printStackTrace();}}// 解析天气结果private void parseWeather(SoapObject detail)throws UnsupportedEncodingException {String date = detail.getProperty(6).toString();weatherToday = "今天天气:" + date.split(" ")[0];weatherToday = weatherToday + "\nsdsd" + date.split(" ")[1];weatherToday = weatherToday + "\nsds"+ detail.getProperty(5).toString();weatherToday = weatherToday + "\n晴朗" + detail.getProperty(7).toString()+ "\n";System.out.println("weatherToday is " + weatherToday);handler.obtainMessage().sendToTarget();// text.setText(weatherToday);Log.e(TAG, "sdsd" + weatherToday);}Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {Log.e(TAG, "handle");// text.setText(detail.toString());text.setText((String) msg.obj);};};}</span>
查询手机号码归属地:




0 0