一个联网小程序,模拟器上可以,在Nokia真机上也可以,但是MOTO真机上却失败,请各位老大帮忙看看,谢谢啦

cafeboy 2008-12-18 11:15:02
自己写的一个MIDLET,访问一个servlet获取一段文字显示在屏幕上,在模拟器上运行没有问题,在Nokia真机上也可以,但是换到Moto真机上却失败,现象是这样的:运行后,提示连接网络,确认后等了一会,屏幕顶端出现一个电话的小图标,闪了一下后,程序就退出了。我也试图catch里面抛出来的Exception,但是错误信息也是null。另外Moto的上网连接我也改成cmnet方式了,听说Moto默认的方式是cmwap,是不是改成cmnet也没有用,还有,我在moto的浏览器里访问这个servlet是可以显示信息的,但是在java程序里访问就出现上面的情况。请各位老大帮忙看看,程序如下:

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

import java.io.*;

public class HttpMidlet extends MIDlet implements Runnable,CommandListener {

// 使用默认的URL。用户可以从图形用户接口改变这个值

private static String defaultURL = "http://127.0.0.1:8000/test";
// 主MIDP 显示

private Display myDisplay = null;
// 输入URL的图形用户接口组件

private Form requestScreen;

private TextField requestField;
// 用于提交请求的图形用户接口组件

private List list;

private String[] menuItems;
// 用于显示服务器响应的图形用户接口组件

private Form resultScreen;

private StringItem resultField;
// 用于requestScreen的"send"按钮

Command sendCommand;

// 用于requestScreen的"exit"按钮

Command exitCommand;

// 用于requestScreen的"back"按钮

Command backCommand;

public HttpMidlet() {

// 初始化图形用户接口组件

myDisplay = Display.getDisplay(this);

sendCommand = new Command("SEND", Command.OK, 1);

exitCommand = new Command("EXIT", Command.OK, 1);

backCommand = new Command("BACK", Command.OK, 1);
// 显示请求的URL

requestScreen = new Form("Type in a URL:");

requestField = new TextField(null, defaultURL, 100, TextField.URL);

requestScreen.append(requestField);

requestScreen.addCommand(sendCommand);

requestScreen.addCommand(exitCommand);

requestScreen.setCommandListener(this);
// 选择想要的HTTP请求方法

menuItems = new String[] { "GET Request", "POST Request" };

list = new List("Select an HTTP method:", List.IMPLICIT, menuItems,
null);

list.setCommandListener(this);
// 先是从服务器上收到的信息

resultScreen = new Form("Server Response:");

resultScreen.addCommand(backCommand);

resultScreen.setCommandListener(this);
}// 结束HttpMidlet()

public void startApp() {
myDisplay.setCurrent(requestScreen);



}// 结束 startApp()

public void commandAction(Command com, Displayable disp) {

// 当用户点击"send"按钮

if (com == sendCommand) {

myDisplay.setCurrent(list);

} else if (com == backCommand) {

requestField.setString(defaultURL);

myDisplay.setCurrent(requestScreen);

} else if (com == exitCommand) {

destroyApp(true);

notifyDestroyed();

}// 结束 if ( com == sendCommand )


if (disp == list && com == List.SELECT_COMMAND) {
Thread funcTh = new Thread(this);
funcTh.start();


}
// 结束if ( dis == list && com == List.SELECT_COMMAND )

}// 结束 commandAction( Command, Displayable )

private String sendHttpGet(String url)

{

HttpConnection hcon = null;

DataInputStream dis = null;

StringBuffer responseMessage = new StringBuffer();
try {

// 使用READ权限的标准的 HttpConnection

hcon = (HttpConnection) Connector.open(url);
// 从HttpConnection取得一个 DataInputStream

dis = new DataInputStream(hcon.openInputStream());
// 从服务器上取回响应

int ch;

while ((ch = dis.read()) != -1) {

responseMessage.append((char) ch);

}// 结束while ( ( ch = dis.read() ) != -1 )

}

catch (Exception e)

{

e.printStackTrace();

responseMessage.append("ERROR:" +e.getMessage());

} finally {

try {

if (hcon != null)
hcon.close();

if (dis != null)
dis.close();

} catch (IOException ioe) {

ioe.printStackTrace();

}// 结束try/catch

}// 结束try/catch/finally

return responseMessage.toString();

}// 结束sendHttpGet( String )

private String sendHttpPost(String url)

{

HttpConnection hcon = null;

DataInputStream dis = null;

DataOutputStream dos = null;

StringBuffer responseMessage = new StringBuffer();

// 请求体

String requeststring = "This is a POST.";
try {

// 使用读写权限的 HttpConnection

hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
// 设置请求方法为POST

hcon.setRequestMethod(HttpConnection.POST);
// 取得发送请求字符串的DataOutputStream

dos = hcon.openDataOutputStream();

byte[] request_body = requeststring.getBytes();
// 发送请求字符串到服务器

for (int i = 0; i < request_body.length; i++) {

dos.writeByte(request_body[i]);

}// 结束 for( int i = 0; i < request_body.length; i++ )
// 取得做为接收服务器响应的DataInputStream

dis = new DataInputStream(hcon.openInputStream());
// 从服务器上取回响应

int ch;

while ((ch = dis.read()) != -1) {

responseMessage.append((char) ch);

}// 结束while( ( ch = dis.read() ) != -1 ) {

}

catch (Exception e)

{

e.printStackTrace();

responseMessage.append("ERROR");

}

finally {

// 释放输入输出流和HTTP连接

try {

if (hcon != null)
hcon.close();

if (dis != null)
dis.close();

if (dos != null)
dos.close();

} catch (IOException ioe) {

ioe.printStackTrace();

}// 结束try/catch

}// 结束try/catch/finally

return responseMessage.toString();

}// 结束sendHttpPost( String )

public void pauseApp() {

}// 结束pauseApp()

public void destroyApp(boolean unconditional) {

myDisplay = null;

requestScreen = null;

requestField = null;

resultScreen = null;

resultField = null;

}// 结束 destroyApp( boolean )
public void run()
{
String result;
if (list.getSelectedIndex() == 0) // 发送一个 GET 请求到服务器

result = sendHttpGet(requestField.getString());

else
// 发送一个 POST 请求到服务器

result = sendHttpPost(requestField.getString());
resultField = new StringItem(null, result);

resultScreen.append(resultField);

myDisplay.setCurrent(resultScreen);
}
}// 结束HttpMidlet

...全文
253 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
cccloveyf 2009-01-13
  • 打赏
  • 举报
回复
是问CMWAP如何联网吗?部分地区移动会有个WML的资费推送页,受到后丢弃,需要二次连接。
huruihappy 2009-01-12
  • 打赏
  • 举报
回复
看完以后发现楼主 有这么一句话
private static String defaultURL = "http://127.0.0.1:8000/test";

把URL设置在外网才能访问
mengmengyq 2008-12-31
  • 打赏
  • 举报
回复
哎,联网的问题,真是头大。
qdhuxp 2008-12-30
  • 打赏
  • 举报
回复
学习!
sforiz 2008-12-30
  • 打赏
  • 举报
回复
这个问题不好说,学习
jiaojunwu 2008-12-30
  • 打赏
  • 举报
回复
hcon = (HttpConnection) Connector.open(url);
你用这个试试!鸟大了,很多问题说不准的!
cafeboy 2008-12-19
  • 打赏
  • 举报
回复
问题还是没有解决啊,请各位老大帮忙分析分析呀,谢谢啦
pbjcc 2008-12-19
  • 打赏
  • 举报
回复
不懂,up
qqlpp 2008-12-19
  • 打赏
  • 举报
回复
不懂,up
cafeboy 2008-12-19
  • 打赏
  • 举报
回复
继续顶啊
lixiurui 2008-12-19
  • 打赏
  • 举报
回复
cmwap要特殊处理。举个例子,你要通过cmwap访问http://www.baidu.com/a/b/c/index.jsp

你需要Connector.open(http://10.0.0.172:80/a/b/c/index.jsp);
下面要设置httpConnection.conn.setRequestProperty("X-Online-Host", "www.baidu.com");
downmoon 2008-12-18
  • 打赏
  • 举报
回复
关注:有时间试一下
cafeboy 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 biaozai06 的回复:]
。。。
// 使用读写权限的 HttpConnection

hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
// 设置请求方法为POST

hcon.setRequestMethod(HttpConnection.POST);
// 取得发送请求字符串的DataOutputStream

dos = hcon.openDataOutputStream();

byte[] request_body = requeststring.getBytes();
// 发送请求字符串到服务器

for (int i = 0; i < request_body.length; i++) {

dos.writeByt…
[/Quote]

已经flush过了,还是不行,另外我也调试过了,程序停留在
hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE);

dos = hcon.openDataOutputStream();
之间
如果 Connector.open(url, Connector.READ_WRITE); 表示连接成功的话,那么似乎问题出在hcon.openDataOutputStream()这一行。这一行会有什么问题呢
biaozai06 2008-12-18
  • 打赏
  • 举报
回复
。。。
// 使用读写权限的 HttpConnection

hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
// 设置请求方法为POST

hcon.setRequestMethod(HttpConnection.POST);
// 取得发送请求字符串的DataOutputStream

dos = hcon.openDataOutputStream();

byte[] request_body = requeststring.getBytes();
// 发送请求字符串到服务器

for (int i = 0; i < request_body.length; i++) {

dos.writeByte(request_body[i]);

}// 结束 for( int i = 0; i < request_body.length; i++ )
dos.flush(); //写完字符串后flush试试

// 取得做为接收服务器响应的DataInputStream
。。。
cafeboy 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yanhan0615 的回复:]


如果是这样的话,就要看看具体的机型差异了,看看中途有没有异常,定位是连接连不上还是连上了没有办法读写数据;
另:在你write数据完成之后flush()一下
[/Quote]

如何定位是不是已经连接上,是不是在hcon = (HttpConnection) Connector.open(url);这行过去后在屏幕上打印一个信息呀
cafeboy 2008-12-18
  • 打赏
  • 举报
回复
我现在使用的是get方式发送数据的,好象没有flush的。
biaozai06 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yanhan0615 的回复:]
。。。
如果是这样的话,就要看看具体的机型差异了,看看中途有没有异常,定位是连接连不上还是连上了没有办法读写数据;
另:在你write数据完成之后flush()一下
[/Quote]

炮炮说的对,每次发送完数据之后最好flush()一下。
有一次我的程序没用flush(),在一个机型上没问题,在另一个机型上试,就不行了。后来加上flush(),一切正常!
yanhan0615 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cafeboy 的回复:]
引用 4 楼 daaoke 的回复:
正确。moto是cmwap.有些机型设了cmnet也没用


我这款v6,设置了cmnet,可以上网,并且也可以在浏览器里访问那个servlet,但是在java程序里就不行,是不是在java程序里moto还是写死了去找cmwap的连接呢,谢谢
[/Quote]

如果是这样的话,就要看看具体的机型差异了,看看中途有没有异常,定位是连接连不上还是连上了没有办法读写数据;
另:在你write数据完成之后flush()一下
cafeboy 2008-12-18
  • 打赏
  • 举报
回复
顶啊
cafeboy 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 daaoke 的回复:]
正确。moto是cmwap.有些机型设了cmnet也没用
[/Quote]

我这款v6,设置了cmnet,可以上网,并且也可以在浏览器里访问那个servlet,但是在java程序里就不行,是不是在java程序里moto还是写死了去找cmwap的连接呢,谢谢
加载更多回复(3)

13,100

社区成员

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

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