【Android开发】网络编程及Internet应用-获取天气预报

简介:
在Eclipse中创建Android项目,利用之前学过的WebView控件和中国天气网提供的天气数据接口,实现获取指定城市的天气预报。

布局文件:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_weight="4"
	    android:gravity="center"
	    android:orientation="horizontal" >
	    <Button 
	        android:id="@+id/beijing"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="北京"/>
	    <Button 
	        android:id="@+id/shanghai"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="上海"/>
	    <Button 
	        android:id="@+id/haerbin"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="哈尔滨"/>
	    <Button 
	        android:id="@+id/changchun"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="长春"/>
	    <Button 
	        android:id="@+id/shenyang"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="沈阳"/>
	    <Button 
	        android:id="@+id/guangzhou"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="广州"/>
    </LinearLayout>	
    
    <LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_weight="1"
	    android:orientation="horizontal" >
	    
	<WebView android:id="@+id/webview1"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"/>
	
	</LinearLayout>
</LinearLayout>

布局效果如图



要在AndroidManifest.xml中设置强制横屏(android:screenOrientation="landscape"):
<activity
    android:name=".MainActivity"
    android:screenOrientation="landscape"
    android:label="@string/app_name" >
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
</activity>

MainActivity:
package com.example.test;  
  
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
  
public class MainActivity extends Activity implements OnClickListener{  
	private WebView webview;
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);
        
        webview=(WebView)findViewById(R.id.webview1);
        //处理各种通知请求和事件,如果不使用该句代码,将使用内置浏览器访问网页
        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);//设置兼容JavaScript
        webview.setWebChromeClient(new WebChromeClient());//处理JavaScript对话框
        //设置默认显示的天气预报信息
        webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm");
        webview.setInitialScale(57*4);//将网页内容放大四倍
        
        Button bj=(Button)findViewById(R.id.beijing);
        bj.setOnClickListener(this);
        Button sh=(Button)findViewById(R.id.shanghai);
        sh.setOnClickListener(this);
        Button hrb=(Button)findViewById(R.id.haerbin);
        hrb.setOnClickListener(this);
        Button cc=(Button)findViewById(R.id.changchun);
        cc.setOnClickListener(this);
        Button sy=(Button)findViewById(R.id.shenyang);
        sy.setOnClickListener(this);
        Button gz=(Button)findViewById(R.id.guangzhou);
        gz.setOnClickListener(this);
    }
    
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.beijing: //单击的是"北京"按钮
			openUrl("101010100T");
			break;
		case R.id.shanghai: //单击的是"上海"按钮
			openUrl("101020100T");
			break;
		case R.id.haerbin: //单击的是"哈尔滨"按钮
			openUrl("101050101T");
			break;
		case R.id.changchun: //单击的是"长春"按钮
			openUrl("101060101T");
			break;
		case R.id.shenyang: //单击的是"沈阳"按钮
			openUrl("101070101T");
			break;
		case R.id.guangzhou: //单击的是"广州"按钮
			openUrl("101280101T");
			break;


		default:
			break;
		}
		
	}


	private void openUrl(String id) {
		//获取并显示天气预报信息
		webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm?id="+id+"");
		
	}
      
}  

别忘记在AndroidManifest.xml中加入访问网络的权限:
<!-- 添加链接网络的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>

运行结果如图


转载请注明出处:http://blog.csdn.net/acmman/article/details/46509511

相关文章
|
4月前
|
机器学习/深度学习 PyTorch TensorFlow
卷积神经网络深度解析:从基础原理到实战应用的完整指南
蒋星熠Jaxonic,深度学习探索者。深耕TensorFlow与PyTorch,分享框架对比、性能优化与实战经验,助力技术进阶。
|
6月前
|
监控 安全 Shell
管道符在渗透测试与网络安全中的全面应用指南
管道符是渗透测试与网络安全中的关键工具,既可用于高效系统管理,也可能被攻击者利用实施命令注入、权限提升、数据外泄等攻击。本文全面解析管道符的基础原理、实战应用与防御策略,涵盖Windows与Linux系统差异、攻击技术示例及检测手段,帮助安全人员掌握其利用方式与防护措施,提升系统安全性。
257 6
|
9月前
|
人工智能 监控 安全
NTP网络子钟的技术架构与行业应用解析
在数字化与智能化时代,时间同步精度至关重要。西安同步电子科技有限公司专注时间频率领域,以“同步天下”品牌提供可靠解决方案。其明星产品SYN6109型NTP网络子钟基于网络时间协议,实现高精度时间同步,广泛应用于考场、医院、智慧场景等领域。公司坚持技术创新,产品通过权威认证,未来将结合5G、物联网等技术推动行业进步,引领精准时间管理新时代。
|
5月前
|
机器学习/深度学习 人工智能 算法
卷积神经网络深度解析:从基础原理到实战应用的完整指南
蒋星熠Jaxonic带你深入卷积神经网络(CNN)核心技术,从生物启发到数学原理,详解ResNet、注意力机制与模型优化,探索视觉智能的演进之路。
523 11
|
8月前
|
JSON 中间件 Go
Go 网络编程:HTTP服务与客户端开发
Go 语言的 `net/http` 包功能强大,可快速构建高并发 HTTP 服务。本文从创建简单 HTTP 服务入手,逐步讲解请求与响应对象、URL 参数处理、自定义路由、JSON 接口、静态文件服务、中间件编写及 HTTPS 配置等内容。通过示例代码展示如何使用 `http.HandleFunc`、`http.ServeMux`、`http.Client` 等工具实现常见功能,帮助开发者掌握构建高效 Web 应用的核心技能。
428 61
|
6月前
|
数据采集 存储 数据可视化
Python网络爬虫在环境保护中的应用:污染源监测数据抓取与分析
在环保领域,数据是决策基础,但分散在多个平台,获取困难。Python网络爬虫技术灵活高效,可自动化抓取空气质量、水质、污染源等数据,实现多平台整合、实时更新、结构化存储与异常预警。本文详解爬虫实战应用,涵盖技术选型、代码实现、反爬策略与数据分析,助力环保数据高效利用。
373 0
|
6月前
|
安全 Linux
利用Libevent在CentOS 7上打造异步网络应用
总结以上步骤,您可以在CentOS 7系统上,使用Libevent有效地构建和运行异步网络应用。通过采取正确的架构和代码设计策略,能保证网络应用的高效性和稳定性。
187 0
|
9月前
|
机器学习/深度学习 算法 测试技术
图神经网络在信息检索重排序中的应用:原理、架构与Python代码解析
本文探讨了基于图的重排序方法在信息检索领域的应用与前景。传统两阶段检索架构中,初始检索速度快但结果可能含噪声,重排序阶段通过强大语言模型提升精度,但仍面临复杂需求挑战
305 0
图神经网络在信息检索重排序中的应用:原理、架构与Python代码解析
|
8月前
|
监控 安全 Linux
AWK在网络安全中的高效应用:从日志分析到威胁狩猎
本文深入探讨AWK在网络安全中的高效应用,涵盖日志分析、威胁狩猎及应急响应等场景。通过实战技巧,助力安全工程师将日志分析效率提升3倍以上,构建轻量级监控方案。文章详解AWK核心语法与网络安全专用技巧,如时间范围分析、多条件过滤和数据脱敏,并提供性能优化与工具集成方案。掌握AWK,让安全工作事半功倍!
291 0
|
8月前
|
人工智能 安全 网络安全
网络安全厂商F5推出AI Gateway,化解大模型应用风险
网络安全厂商F5推出AI Gateway,化解大模型应用风险
271 0