?? weatherreader.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){
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -