一个简单的联网问题:

koolgi 2003-01-29 04:13:01
我的开发环境是:J2ME Version 1.0.3 Beta,J2SE Version 1.3.0_01,apache

1.3.27,并配置好apache,在IE上输入http://127.0.0.1:8080/a.html能正常显示
我输入以下代码:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class HTTPTest extends MIDlet {

private Display display;

/** Constructor */
public HTTPTest() {
display=Display.getDisplay(this);
}
/** Main method */
public void startApp() {
try{
String url="http://127.0.0.1:8080/a.html";
HttpConnection hc=(HttpConnection)Connector.open(url);
DataInputStream dis=new

DataInputStream(hc.openDataInputStream());
String content="";
int ic;
while((ic=dis.read())!=-1) content=content+(char)ic;
Form f=new Form("HTTP Test");
f.append(content);
display.setCurrent(f);
}catch(Exception e){
System.out.println(e);
notifyDestroyed();
}
}
/** Handle pausing the MIDlet */
public void pauseApp() {
}
/** Handle destroying the MIDlet */
public void destroyApp(boolean unconditional) {
}
/** Quit the MIDlet */
}
当我编译,运行后,点击Launch后,出现

javax.microedition.io.ConnectionNotFoundException错误,请高手指点。
...全文
52 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
koolgi 2003-02-14
  • 打赏
  • 举报
回复
成功了,谢谢大家!
goes_kad 2003-02-04
  • 打赏
  • 举报
回复
我也有同样的问题,的确是DataInputStream dis=new DataInputStream(hc.openDataInputStream());出的问题,实际上也就是hc.openDataInputStream();出的问题,可能是根本没有连接到服务器,代码本身并没有问题
koolgi 2003-02-01
  • 打赏
  • 举报
回复
我把try那段改成:
String url="http://127.0.0.1:8080/a.html";
try{
System.out.println("Before Http");
HttpConnection hc=(HttpConnection)Connector.open(url);
System.out.println("After Http");
System.out.println("Before DataInput");
DataInputStream dis=new DataInputStream(hc.openDataInputStream());
System.out.println("after DataInput");
String content="";
int ic;
while((ic=dis.read())!=-1) content=content+(char)ic;
Form f=new Form("HTTP Test");
f.append(content);
display.setCurrent(f);
}catch(IOException e){
System.out.println(e);
notifyDestroyed();
}
}

编译后的结果是:

Before HttpAfter HttpBefore DataInputjavax.microedition.io.ConnectionNotFoundException

看来是DataInputStream dis=new DataInputStream(hc.openDataInputStream());这句话的问题,可是我不知如何做了。
koolgi 2003-01-31
  • 打赏
  • 举报
回复
villagehead(村长) 的方法不行,编译通过不了的。
羊年大吉
villagehead 2003-01-30
  • 打赏
  • 举报
回复
你的hc和dis是在前一个{}里面定义的,
除了这个{},就会没有作用了!

看来我是出了一个馊主意!

我的意思是:
原来:
try{
语句1
语句2
语句3
语句4
}catch(){}

变成

语句1
try{
语句2
语句3
语句4
}catch(){}

或者

语句1
语句2
try{
语句3
语句4
}catch(){}

btw:这是原创绝对的馊主意!

新年快乐!
MrYoucheng 2003-01-30
  • 打赏
  • 举报
回复
这个注意是不行的
因为有的语句没有try通不过编译
我觉的问题在HttpConnection hc=(HttpConnection)Connector.open(url);
这句话
不信在这句话前加System.out.println("before");
后加system.out.println("after");
显示了before就会有错误
koolgi 2003-01-29
  • 打赏
  • 举报
回复
我家里就一台机子,我安装了apache。
koolgi 2003-01-29
  • 打赏
  • 举报
回复
build的时出现下面错误:
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:31: cannot resolve symbol
symbol : variable hc
location: class HTTPTest
DataInputStream dis=new DataInputStream(hc.openDataInputStream());
^
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:41: cannot resolve symbol
symbol : variable dis
location: class HTTPTest
while((ic=dis.read())!=-1) content=content+(char)ic;
^
2 errors
com.sun.kvem.ktools.ExecutionException
koolgi 2003-01-29
  • 打赏
  • 举报
回复
你的意思是不是这样:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class HTTPTest extends MIDlet {

private Display display;


/** Constructor */
public HTTPTest() {
display=Display.getDisplay(this);
}

/** Main method */
public void startApp() {

String url="http://127.0.0.1:8080/a.html";
try{
HttpConnection hc=(HttpConnection)Connector.open(url);
}catch(Exception e){
System.out.println("HttpConnection"+e);
//notifyDestroyed();
}

try{
DataInputStream dis=new DataInputStream(hc.openDataInputStream());
}catch(Exception e){
System.out.println("DataInputStream"+e);
//notifyDestroyed();
}

String content="";
int ic;

try{
while((ic=dis.read())!=-1) content=content+(char)ic;
}catch(Exception e){
System.out.println("ic=dis.read()"+e);
//notifyDestroyed();
}
Form f=new Form("HTTP Test");
f.append(content);
display.setCurrent(f);
}

/** Handle pausing the MIDlet */
public void pauseApp() {
}

/** Handle destroying the MIDlet */
public void destroyApp(boolean unconditional) {
}

/** Quit the MIDlet */

}
villagehead 2003-01-29
  • 打赏
  • 举报
回复
我的意思不是删除try{
而是把try{里的语句一行一行的放到try{上面,
来定位到底是那句话抛出了exception!

强烈建议你换j2mewtk1.0.4试试!
MrYoucheng 2003-01-29
  • 打赏
  • 举报
回复
你是不是局育网,哪个可能会有问题的
try是不能删的 因为要求捕捉错误,不然会编译不通过
koolgi 2003-01-29
  • 打赏
  • 举报
回复
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:23: unreported exception java.io.IOException; must be caught or declared to be thrown
HttpConnection hc=(HttpConnection)Connector.open(url);
^
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:24: unreported exception java.io.IOException; must be caught or declared to be thrown
DataInputStream dis=new DataInputStream(hc.openDataInputStream());
^
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:27: unreported exception java.io.IOException; must be caught or declared to be thrown
while((ic=dis.read())!=-1) content=content+(char)ic;
^
3 errors
com.sun.kvem.ktools.ExecutionException



koolgi 2003-01-29
  • 打赏
  • 举报
回复
我删掉了try-catch语句。结果出现下面3个错误,请高手分析一下:

e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:23: unreported exception java.io.IOException; must be caught or declared to be thrown
HttpConnection hc=(HttpConnection)Connector.open(url);
^
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:24: unreported exception java.io.IOException; must be caught or declared to be thrown
DataInputStream dis=new DataInputStream(hc.openDataInputStream());
^
e:\J2mewtk\apps\HTTP Test\src\HTTPTest.java:27: unreported exception java.io.IOException; must be caught or declared to be thrown
while((ic=dis.read())!=-1) content=content+(char)ic;
^
3 errors
com.sun.kvem.ktools.ExecutionException
koolgi 2003-01-29
  • 打赏
  • 举报
回复
thanks,i try!
villagehead 2003-01-29
  • 打赏
  • 举报
回复
或者是你的j2me环境出问题了!
你用j2me wtk 1.0.4试试
villagehead 2003-01-29
  • 打赏
  • 举报
回复
我这里运行你的程序没错呀!

你把完整的出错信息贴上来看看!

或者你移动那个try{,把出错的语句找出来!
再看看!

13,097

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧