获取城市天气信息的简单例子-----WebService
发布时间:2020-12-16 23:30:06 所属栏目:安全 来源:网络整理
导读:本例是通过第三方类库ksoap2来调用天气WebService的简单实例 前提: 1.下载kSoap2.jar包; 2.在AndroidManifest.xml文件中声明网络访问权限 uses-permission android:name="android.permission.INTERNET" / //这句话放在/activity标签后面即可; 先上代码:
本例是通过第三方类库ksoap2来调用天气WebService的简单实例 前提: 1.下载kSoap2.jar包; 2.在AndroidManifest.xml文件中声明网络访问权限 <uses-permission android:name="android.permission.INTERNET" />//这句话放在</activity>标签后面即可; 先上代码: 1.activity_main.xml代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/cityName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入城市名称" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/getWeather" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取天气信息" /> </LinearLayout> 2.MainActivity代码: import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import android.os.Build; public class MainActivity extends ActionBarActivity { final String NAMESPACE ="http://WebXml.com.cn/"; //webService地址 final String URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; final String METHOD_NAME = "getWeatherbyCityName"; final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; private Button btnWeather; private EditText cityName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnWeather = (Button) findViewById(R.id.getWeather); cityName = (EditText) findViewById(R.id.cityName); btnWeather.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getWeather(); } }); } private void getWeather(){ Thread thread = new Thread( new Runnable() { //@Override public void run() { try { //获取输入的城市名称 String cityname = cityName.getText().toString(); final String NAMESPACE ="http://WebXml.com.cn/"; //webService地址 final String URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; final String METHOD_NAME = "getWeatherbyCityName"; final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; //命名空间+方法名 // 创建HttpTransportSE对象,传递WebService服务器地址 HttpTransportSE ht = new HttpTransportSE(URL); //1.创建SoapObject 并指定访问的名称空间及方法名 SoapObject sop = new SoapObject(NAMESPACE,METHOD_NAME); //2.设置参数值 sop.addProperty("theCityName",cityname); /** 常量SoapEnvelope.VER10:对应于SOAP 1.0规范 * 常量SoapEnvelope.VER11:对应于SOAP 1.1规范 * 常量SoapEnvelope.VER12:对应于SOAP 1.2规范 * 这样,无论要调用的webservice采用了哪一个SOAP规范,你都可以轻松应对。 * */ // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号 SoapSerializationEnvelope senp = new SoapSerializationEnvelope(SoapEnvelope.VER11); //设置是否调用.net开发的webService senp.dotNet = true; //设置对象的bodyOut属性 senp.setOutputSoapObject(sop);//senp.bodyOut = sop; ht.debug=true; //执行call方法 发送请求 //ht.call(null,senp); ht.call(SOAP_ACTION,senp); //获取结果 SoapObject obj = (SoapObject) senp.bodyIn; SoapObject detail = (SoapObject) obj.getProperty("getWeatherbyCityNameResult"); Log.i("MyTag",detail.toString()); //效果 : 会输出从网络获取的对应城市的天气信息 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); Log.i("MyTag","start"); thread.start(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |