Android开发WeatherForecast程序

[b]1,使用Googel API[/b]
[url]http://www.google.com/ig/api?&weather=beijing[/url]

[b]2,使用DOM解析XML[/b]
[code]
String weather = "";
String url = "http://www.google.com/ig/api?&weather=beijing";
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest req = new HttpGet(url);
HttpResponse resp = client.execute(req);
HttpEntity ent = resp.getEntity();
InputStream stream = ent.getContent();
DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document d = b.parse(new InputSource(stream));
NodeList n = d.getElementsByTagName("forecast_conditions");
for (int i = 0; i < n.getLength(); i++) {
weather += n.item(i).getChildNodes().item(0).getAttributes().item(0).getNodeValue();
weather += ", ";
weather += (Integer.parseInt(n.item(i).getChildNodes().item(1).getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;
weather += " ~ ";
weather += (Integer.parseInt(n.item(i).getChildNodes()
.item(2).getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;
weather += ", ";
weather += n.item(i).getChildNodes().item(4).getAttributes().item(0).getNodeValue();
weather += "\n";
}
[/code]

[b]3,另起Thread处理Web请求[/b]
[code]
new Thread() {
public void run() {
try {
// ...
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
[/code]

[b]4,使用Handler传递Thread处理结果到UI主Thread[/b]
[code]
h = new Handler() {
public void handleMessage(Message msg) {
tv.setText((String)msg.obj);
}
};

new Thread() {
// ...
Message msg = h.obtainMessage(1, 1, 1, weather);
h.sendMessage(msg);
}
[/code]

[b]5,完整代码[/b]
[code]
package com.hideto.weatherforecast;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class WeatherForecast extends Activity {

public TextView tv;
public Handler h;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("Loading...");
setContentView(tv);
h = new Handler() {
public void handleMessage(Message msg) {
tv.setText((String)msg.obj);
}
};
new Thread() {
public void run() {
try {
String weather = "";
String url = "http://www.google.com/ig/api?&weather=beijing";
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest req = new HttpGet(url);
HttpResponse resp = client.execute(req);
HttpEntity ent = resp.getEntity();
InputStream stream = ent.getContent();
DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document d = b.parse(new InputSource(stream));
NodeList n = d.getElementsByTagName("forecast_conditions");
for (int i = 0; i < n.getLength(); i++) {
weather += n.item(i).getChildNodes().item(0)
.getAttributes().item(0).getNodeValue();
weather += ", ";
weather += (Integer.parseInt(n.item(i).getChildNodes()
.item(1).getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;
weather += " ~ ";
weather += (Integer.parseInt(n.item(i).getChildNodes()
.item(2).getAttributes().item(0).getNodeValue()) - 32) * 5 / 9;
weather += ", ";
weather += n.item(i).getChildNodes().item(4)
.getAttributes().item(0).getNodeValue();
weather += "\n";
}
Message msg = h.obtainMessage(1, 1, 1, weather);
h.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
[/code]

[b]6,修改AndroidManifest.xml加上INTERNET访问权限[/b]
[code]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hideto.weatherforecast"
android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WeatherForecast"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值