随笔-348  评论-598  文章-0  trackbacks-0
今天做黑莓平台上J2ME的Socket测试程序,服务端已经调试好了,服务端也用模拟器调试好了,可到了黑莓上面就有问题了,程序运行正常,连接也正常建立,但发送出去的数据服务端就是收不到导致阻塞,服务端阻塞也导致了客户端接收数据的阻塞。。。死活没发现原因。。。最后测试来测试去才发现问题所在。
之前的程序:
SocketConnection conn = null;
                    OutputStream os 
= null;
                    InputStream is 
= null;
                    
try {
                        getUrl();
                        
if ("".equals(url.trim()) || "".equals(port)) {
                            log(
"请将Socket测试地址和端口填写完整", Thread.currentThread());
                            
return;
                        }

                        
try {
                            Integer.parseInt(port);
                        }
 catch (Exception e) {
                            log(
"端口号必须是整数", Thread.currentThread());
                            
return;
                        }

                        log(CONNECTING_STR, Thread.currentThread());
                        conn 
= (SocketConnection) Connector.open("socket://" + url + ":" + port, Connector.READ_WRITE, true);
                        conn.setSocketOption(SocketConnection.LINGER, 
5);

                        
// 发送和回复的请求
                        os = conn.openDataOutputStream();
                        is 
= conn.openDataInputStream();
修正之后的程序:
SocketConnection conn = null;
                    DataOutputStream os 
= null;
                    DataInputStream is 
= null;
                    
try {
                        getUrl();
                        
if ("".equals(url.trim()) || "".equals(port)) {
                            log(
"请将Socket测试地址和端口填写完整", Thread.currentThread());
                            
return;
                        }

                        
try {
                            Integer.parseInt(port);
                        }
 catch (Exception e) {
                            log(
"端口号必须是整数", Thread.currentThread());
                            
return;
                        }

                        log(CONNECTING_STR, Thread.currentThread());
                        conn 
= (SocketConnection) Connector.open("socket://" + url + ":" + port, Connector.READ_WRITE, true);
                        conn.setSocketOption(SocketConnection.LINGER, 
5);

                        
// 发送和回复的请求
                        os = conn.openDataOutputStream();
                        is 
= conn.openDataInputStream();
看出来了吗?
os和is变量的声明类型不一样,如果用OutputStream和InputStream这一类的抽象类声明只能调用标准读写接口,在J2ME模拟器上面没有问题,但是到了黑莓上面死活都不行,后来用了具体实现类来声明,调用了ReadUTF和WriteUTF来进行读写操作,就没有任何问题了。

---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2010-01-10 22:16 TiGERTiAN 阅读(1927) 评论(1)  编辑  收藏 所属分类: JavaJ2MEBlackBerry

评论:
# re: BlackBerry让我差点把电脑扔了。。。 2010-01-11 20:14 | javacool
呵呵,编码有时候会把人郁闷死,希望UTF-8早日一统江湖!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: