就是自己练习一个很简单的获取一个网络上文本数据应用,获取后,显示在TextBox上,不知道为什么显示空白。麻烦大家看下哪里有问题。[code=Java][/code]public class weather extends MIDlet {
private TextBox textbox;
public weather() throws IOException {
super();
String info = "";
String preWeather = "";
final String TITLE = "短期预报";
DataInputStream in = null;
HttpConnection wConnection = null;
String URL = "http://www.hzqx.com/gzhfw/dqyb.asp";
try {
wConnection = (HttpConnection)Connector.open(URL);
wConnection.setRequestMethod(HttpConnection.GET);
in = new DataInputStream(wConnection.openInputStream());
int character;
while((character = in.read())!= -1)
{
info += (char)character;
}
}catch (IOException e) {
System.out.print("ERROR:" + e);
}
finally
{
if (wConnection != null){
wConnection.close();
wConnection = null;
}
if (in != null){
in.close();
}
}
info = (unicodeTogb2312(info)).trim();
preWeather = GetWeatherInfomation(info);
textbox = new TextBox(TITLE,preWeather,20,0);
}
public static String unicodeTogb2312(String s){
if(s == null){return "";}
if(s.equals("")){return s;}
try{
return new String(s.getBytes("ISO8859_1"),"gb2312");
}catch(Exception e){
return s;
}
}
public static String GetWeatherInfomation(String str){
String preWeather = "";
String td = "今";
String end = "更";
int startNum;
int endNum;
startNum = str.indexOf(td);
endNum = str.indexOf(end);
if (startNum == -1){
preWeather = "获取失败";
return preWeather;
}
else {
preWeather = str.substring(startNum, endNum);
return preWeather;
}
}
不知道哪里出问题了。显示"获取失败",没内容。电脑可以上网。
Eclipse里关掉模拟器似乎有这么一句ERROR显示:ERROR:javax.microedition.io.ConnectionNotFoundException: TCP openExecution completed
------解决方案--------------------------------------------------------
tcp open应该是电脑的网络问题吧,你多试几下。。
------解决方案--------------------------------------------------------
000000000000000