android調用天氣預報的webservice例子

來源:互聯網
上載者:User

在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 指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.