安卓实现天气预报功能
来源:互联网 发布:定义学生数组java 编辑:程序博客网 时间:2024/05/17 21:06
此篇文章主要针对初学者,讲述了如何如何利用webservice访问天气预报接口获取网络数据,对返回结果的处理,以及如何利用Handler更新主线程UI。先看效果图:
接下来看主程序:
package com.example.getweather;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.annotation.SuppressLint;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Color;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private EditText cityname;private Button btn;private TextView textView; private final int duration=Toast.LENGTH_LONG; private String theCityName; private String result; private ProgressDialog progressDialog; @SuppressLint("HandlerLeak")private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {progressDialog.dismiss();switch (msg.what) {case 1:LinearLayout myLayout=(LinearLayout) findViewById(R.id.myLinearLayout);PraseWeaTherUtil p=new PraseWeaTherUtil(result.substring(8, result.length()-2));textView.setText("所在省/直辖市:"+p.getProvince());myLayout.addView(getTextView("所在市:"+p.getCity()));myLayout.addView(getTextView("时间:"+p.getTime()));myLayout.addView(getTextView("温度:"+p.getNowDayTemperature()));myLayout.addView(getTextView("今日天气:"+p.getNowDayWeaTher()));myLayout.addView(getTextView("天气实况:"+p.getNowDayWeaTher_Detail()));myLayout.addView(getTextView("风力:"+p.getWind_power()));myLayout.addView(getTextView("明日天气:"+p.getWeaTher_Detail2()));myLayout.addView(getTextView("温度:"+p.getTemperature2()));myLayout.addView(getTextView("后天天气:"+p.getWeaTher_Detail3()));myLayout.addView(getTextView("温度:"+p.getTemperature3()));break;case -1:Toast.makeText(getApplicationContext(), "获取数据失败", duration).show();break;}} }; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView=(TextView)findViewById(R.id.textview);cityname=(EditText)findViewById(R.id.cityname);btn=(Button)findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {theCityName=cityname.getText().toString();if(null==theCityName||"".equals(theCityName)){Toast.makeText(getApplicationContext(), "请填写要查询的城市", duration).show();return;}progressDialog=new ProgressDialog(MainActivity.this);progressDialog.setMessage("正在获取天气数据...");progressDialog.show();new Thread(new GetWeatherTask(theCityName)).start();}});}private class GetWeatherTask implements Runnable{String theCityName="";public GetWeatherTask(String theCityName) {super();this.theCityName = theCityName;}@Overridepublic void run() {try{result=getRemoteInfo(theCityName);handler.obtainMessage(1).sendToTarget();}catch(Exception e){e.printStackTrace();handler.obtainMessage(-1).sendToTarget();}}}private TextView getTextView(String content){TextView tv=new TextView(this);tv.setTextSize(16);tv.setTextColor(Color.parseColor("#000000"));tv.setText(content);return tv;}/** * 通过webservice获取城市天气 * @param theCityName * @return */public String getRemoteInfo(String theCityName) {//http://WebXml.com.cn/getWeatherbyCityName// 命名空间String nameSpace = "http://WebXml.com.cn/";// 调用的方法名称String methodName = "getWeatherbyCityName";// EndPoint通常是将WSDL地址末尾的"?WSDL"去除后剩余的部分String endPoint = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";// SOAP Action通常为命名空间 + 调用的方法名称String soapAction = "http://WebXml.com.cn/getWeatherbyCityName";// 指定WebService的命名空间和调用的方法名SoapObject rpc = new SoapObject(nameSpace, methodName);// 设置需调用WebService接口需要传入的两个参数mobileCode、userIdrpc.addProperty("theCityName", theCityName);// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);envelope.bodyOut = rpc;// 设置是否调用的是dotNet开发的WebServiceenvelope.dotNet = true;// 等价于envelope.bodyOut = rpc;envelope.setOutputSoapObject(rpc);HttpTransportSE transport = new HttpTransportSE(endPoint);try {// 调用WebServicetransport.call(soapAction, envelope);} catch (Exception e) {e.printStackTrace();}// 获取返回的数据SoapObject object = (SoapObject) envelope.bodyIn;return object.getProperty(0).toString();}}
public PraseWeaTherUtil(String result) {super();String results[]=result.replace("string=", "").split(";");this.province=results[0];this.city=results[1];this.time=results[4];this.nowDayTemperature=results[5];this.nowDayWeaTher=results[6];this.nowDayWeaTher_Detail=results[10];this.WeaTher_Detail2=results[13];this.temperature2=results[12];this.WeaTher_Detail3=results[18];this.temperature3=results[17];}
详细解释已经在代码中写的很清楚,最后不要忘了写入访问网络权限,大家可以下载我的源码参考:
http://download.csdn.net/detail/baiyuliang2013/7091937
21 1
- 安卓实现天气预报功能
- 安卓Web Service实现天气预报功能
- python 实现天气预报功能
- 实现天气预报功能
- 安卓 天气预报
- 调用API实现天气预报功能!
- 调用API实现天气预报功能
- delphi Indy 实现天气预报功能
- 实现简单的天气预报功能
- 安卓实现拨号功能
- 安卓抽奖功能实现
- 安卓实现复读功能
- Flex 调用WEBService 接口实现天气预报功能.
- 天气预报功能--查询广州天气的实现
- 天气预报--查询其他城市天气功能实现
- 微信公众平台实现天气预报功能
- Android:使用AsyncTask实现天气预报功能
- 安卓打电话功能的实现
- 01背包问题的各种解决方案
- g++和gcc区别
- Windows环境变量
- 手把手教您开发JAVA微信SDK
- android 自定义view的overscroll edge color
- 安卓实现天气预报功能
- js与php中三等号(===)和双等号(==)的区别
- hibernate tools for eclipse
- sqlserver 和Oracle中 order by(排序)的区别
- 做好用户体验不得不说的那些事
- ACM组队训练ubuntu环境配置
- 有价值的内容,哪些才能真正吸引用户呢?
- 关卡开启方式以及封闭性
- 第三周作业