Android开发之初级开发_利用第三方包获取天气预报
//本demo基于soap协议;
//需要下载和导入第三方包:ksoap2-android-2.5.8
//可能因为网络的原因数据不是立马显示,需要等待
源码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hql.soap.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/tv_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/tv_01" android:text="搜索" /> <EditText android:id="@+id/cityNmae" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:hint="城市名字" /> </RelativeLayout>
package com.hql.soap; import java.io.UnsupportedEncodingException; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.R.integer; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { // 指定命名空间 private static final String NAMESPACE = "http://WebXml.com.cn/"; // 接口地址 private static final String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; // 设置方法名 private static final String METHOD_NAME = "getWeatherbyCityName"; // 设置查询接口参数 private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; // 定义字符串,保存天气信息 private String weatherToday; private Button okbButton; private SoapObject detail; private EditText cityNmaeText; private TextView cityMsgView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); okbButton = (Button) findViewById(R.id.ok); cityMsgView = (TextView) findViewById(R.id.tv_01); cityNmaeText = (EditText) findViewById(R.id.cityNmae); okbButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { new showWeatherAsyncTask().execute(); } }); } private class showWeatherAsyncTask extends AsyncTask<String, integer, String> { protected String doInBackground(String... params) { showWeather(); return null; } protected void onPostExecute(String string) { } } private void showWeather() { // 获取需要查询的城市名称 String city = cityNmaeText.getText().toString().trim(); if (!city.isEmpty()) { getWeather(city); } else { Toast.makeText(getApplicationContext(), "请输入要查询的城市名称", Toast.LENGTH_SHORT).show(); } } private void getWeather(String cityName) { try { // 新建soapobject对象 SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); rpc.addProperty("theCityName", cityName); HttpTransportSE ht = new HttpTransportSE(URL); ht.debug = true; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); ht.call(SOAP_ACTION, envelope); SoapObject result = (SoapObject) envelope.bodyIn; detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult"); System.out.println("detaile" + detail); parseWeather(detail); return; } catch (Exception e) { e.printStackTrace(); } } private void parseWeather(SoapObject detail) throws UnsupportedEncodingException { String date = detail.getProperty(6).toString(); weatherToday = "今天:" + date.split("")[0]; weatherToday = weatherToday + "天气:" + date.split("")[1]; weatherToday = weatherToday + "气温:" + detail.getProperty(5).toString(); weatherToday = weatherToday + "风力:" + detail.getProperty(7).toString() + ""; cityMsgView.setText(weatherToday); } }
权限:
<uses-permission android:name="android.permission.INTERNET"/>

浙公网安备 33010602011771号