📄 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){
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -