⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weatherreader.java

📁 J2ME实现天气查找的功能的代码。通过手机GPRS可以查找天气
💻 JAVA
字号:
package com.j2medev.chapter4;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.Form;

public class WeatherReader implements Runnable{
    
    private WeatherMIDlet midlet;
    private String param;
    public static final String host = "http://localhost:8080/yahooweather/weather?";
    
    public WeatherReader(WeatherMIDlet midlet,String param) {
        this.midlet = midlet;
        this.param = param;
    }
    
    public void run(){
        HttpConnection conn = null;
        try{
            WaitingForm wait = new WaitingForm();
            midlet.setCurrent(wait);
            wait.update();
            conn = (HttpConnection)Connector.open(host+param);
            conn.setRequestMethod(HttpConnection.GET);
            int responseCode = conn.getResponseCode();
            wait.update();
            if(responseCode != HttpConnection.HTTP_OK){
                //简单起见,只要返回码不等于200则抛出异常
                throw new IOException("Can't connect to server");
            }
            DataInputStream dis = conn.openDataInputStream();
            String result = dis.readUTF();
            wait.update();
            midlet.displayWeather(result);
        }catch(IOException ex){
            //出错提示信息
            Form form = new Form("error");
            form.append(ex.getMessage());
            midlet.setCurrent(form);
        }finally{
            try{
                if(conn != null)
                    conn.close();
            }catch(IOException e){
                
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -