• Android开发WeatherForecast程序


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

    2,使用DOM解析XML

    String weather = "";
     
    String url = "[url=http://www.google.com/ig/api?&weather=beijing]http://www.google.com/ig/api?&weather=beijing[/url]";
     
    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";
     
    }
     

    3,另起Thread处理Web请求

    new Thread() {
     
    public void run() {
       try {
        // ...
       } catch (Exception e) {
        e.printStackTrace();
       }
     }
     
    }.start();
     

    4,使用Handler传递Thread处理结果到UI主Thread

    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);
     
    }
     

    5,完整代码

    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 = "[url=http://www.google.com/ig/api?&weather=beijing]http://www.google.com/ig/api?&weather=beijing[/url]";
     
         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(); } }

    6,修改AndroidManifest.xml加上INTERNET访问权限

    <?xml version="1.0" encoding="utf-8"?>
     
    <manifest xmlns:android="[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]"
           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> 
  • 相关阅读:
    [bzoj3295][Cqoi2011][动态逆序对] (树套树)
    [bzoj3209][花神的数论题] (数位dp+费马小定理)
    [bzoj1026][SCOI2009][windy数] (数位dp)
    [bzoj4521][Cqoi2016][手机号码] (数位dp+记忆化搜索)
    [bzoj1833][ZJOI2010][count] (数位dp)
    [spoj1182][Sorted Bit Sequence] (数位dp)
    [ural1057][Amount of Degrees] (数位dp+进制模型)
    [hdu3652][B-number] (数位dp)
    【bzoj2523】【CTSC2001】聪明的学生
    友情链接(有的是单向的)
  • 原文地址:https://www.cnblogs.com/vus520/p/2561961.html
Copyright © 2020-2023  润新知