在Android SDK中並沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。我用的ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
在匯入包出錯時可參考我上一篇部落格
:
詳細代碼:
package cn.bzu.weatherforecast.soap;import java.io.IOException;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;public class SOAPUtil {public static Object doTransport(final String wsdlUrl, final String webMethod,String cityName){String nameSpace = "http://WebXml.com.cn/";//WebService 的命名空間SoapObject soapObject = new SoapObject(nameSpace, webMethod);soapObject.addProperty("theCityName", cityName);//設定調用方法的參數值System.out.println(cityName);//產生調用Webservice方法的SOAP請求資訊SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);soapSerializationEnvelope.bodyOut = soapObject;soapSerializationEnvelope.dotNet=true;soapSerializationEnvelope.setOutputSoapObject(soapObject);//建立HttpTransportsSE對象。HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);httpTransportSE.debug=true;String SOAP_ACTION = "http://WebXml.com.cn/"+webMethod;System.out.println(SOAP_ACTION);try {//使用call方法調用WebService方法httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);//獲得WebService方法的返回結果if(soapSerializationEnvelope.getResponse() != null){SoapObject result = (SoapObject) soapSerializationEnvelope.getResponse();System.out.println("result:"+result);return result;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}
package cn.bzu.weatherforecast;import org.ksoap2.serialization.SoapObject;import cn.bzu.weatherforecast.soap.SOAPUtil;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText cityText;private Button serach;private TextView resultView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cityText=(EditText) findViewById(R.id.city); serach=(Button) findViewById(R.id.search); resultView=(TextView) findViewById(R.id.result); serach.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String cityName=cityText.getText().toString();String wsdlUrl ="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";String method="getWeatherbyCityName";//WebService 的調用方法SoapObject result=(SoapObject) SOAPUtil.doTransport(wsdlUrl, method, cityName);System.out.println("result:"+result);resultView.setText(result.getProperty(10).toString());}}); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
布局檔案:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/city" /><EditText android:id="@+id/city" android:layout_width="fill_parent"android:layout_height="wrap_content" /><Button android:id="@+id/search" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/search" /><TextView android:id="@+id/result" android:layout_width="fill_parent"android:layout_height="wrap_content" /></LinearLayout>
出錯:java.lang.NullPointerException
雙擊錯誤,可發現出錯代碼是resultView.setText(result.getProperty(10).toString());
分析:那就是resultView或result是null 指標,而resultView是尋找組建,出錯可能性小,所以很可能是resultnull 指標。可以吧result輸出,結果發現result為null,那就要查看代碼:SoapObject result=(SoapObject) SOAPUtil.doTransport(wsdlUrl, method, cityName);查看wsdlUrl是否能開啟,method是否調用正確,最終考慮SOAPUtil.doTransport方法是否正確
最後別忘記:在AndroidManifest.xml中設定許可權<uses-permission android:name="android.permission.INTERNET" ></uses-permission>否則,也可能會報null 指標