关于Nokia 频繁打开HttpConnection 的错误 java.io.IOException: Error in HTTP operation

bzwlq 2009-09-08 04:01:39
在其他的机器上和模拟器上程序都能正常的运行 就在NOKIA机器上出现问题 报 java.io.IOException: Error in HTTP operation

我简单介绍下我程序的意思:

1、HttpConntSend类是发送消息类;(消息通过post提交到服务器上去就关掉流,和连接)
2、HttPConnRrecieve类是读取消息类 ( 隔3秒钟就发送消息到服务器上去 ,服务器就会返回消息 然后解析返回的数据)

代码:

//HttPConnRrecieve 类是读取消息类
package cim.mobile.net.cmwap;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import cim.mobile.CimParam;
import cim.mobile.net.socket.message.CimAbstractMessage;
import cim.mobile.net.socket.message.CimMessageFactory;
import cim.mobile.net.socket.message.CimMessageListener;
import cim.mobile.vo.SystemParams;

public class HttPConnRrecieve extends Thread {
private HttpConnection hc = null;

private CimMessageListener messageListener = null;

private DataInputStream dis;

private DataOutputStream dos;

private static HttPConnRrecieve instance = null;

public void start(CimMessageListener messageListener) {

this.messageListener = messageListener;
Thread t = new Thread(this);
t.start();
}

public static HttPConnRrecieve getInstance() {
if (instance == null) {
instance = new HttPConnRrecieve();
}
return instance;
}

public void recieveMessage(String msgText) {
CimAbstractMessage cam = CimMessageFactory.createMessage(msgText);
if (messageListener != null && cam != null) {
messageListener.dealMessage(cam);
}
}

public void run() {
try {
while (true) {
// hc = (HttpConnection) Connector.open("http://10.0.0.172:80/",
// Connector.READ_WRITE, true);
// hc.setRequestProperty("X-Online-Host", CimParam.getInstance()
// .getSocketHost()
// + ":18888");
hc = (HttpConnection) Connector.open("http://221.130.184.139:18888");
try {
hc.setRequestMethod(HttpConnection.POST);
dos = hc.openDataOutputStream();
String sendXml = "MYDATA: <cim client='cs' type='cmwapRecv' sessi/>\r\n";
dos.write(sendXml.getBytes());
dos.flush();
dos.close();
dis = hc.openDataInputStream();
byte[] buffer;
int size = (int) hc.getLength();
if (size != -1)
buffer = new byte[size]; // 响应大小已知,确定缓冲区大小
else
buffer = new byte[8192 * 4]; // 响应大小未知,设定一个固定大小的缓冲区
int reads = 0;
int index = 0;
while (reads >= 0) {
int len = buffer.length - index;
if (len <= 0) {
break;
}
len = len > 128 ? 128 : len;
reads = dis.read(buffer, index, len);
if (reads <= 0)
break;
index += reads;
}
try {
dis.close();
} catch (IOException ioe) {
}
if (hc != null) {
try {
hc.close();
hc = null;
} catch (IOException ioe) {

}
}
String result = new String(buffer, 0, index, "UTF-8");
recieveMessage(result.toString());
} finally {
if (hc != null) {
hc.close();
hc = null;
}

}
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}
} catch (IOException e) {
e.printStackTrace();
}

}


}






HttpConntSend类是发送消息类

package cim.mobile.net.cmwap;

import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class HttpConntSend extends Thread {
private HttpConnection hc;

private String sMess;

private DataOutputStream dos;

public synchronized void run() {
try {
// hc = (HttpConnection) Connector.open("http://10.0.0.172:80/",
// Connector.READ_WRITE, true);
// hc.setRequestProperty("X-Online-Host", CimParam.getInstance()
// .getSocketHost()+":18888");
hc = (HttpConnection) Connector
.open("http://221.130.184.139:18888");
hc.setRequestMethod(HttpConnection.POST);
dos = hc.openDataOutputStream();
String sendXml = "MYDATA: " + sMess;
System.out.println(sMess);
dos.write(sendXml.getBytes());
dos.flush();
// dis = hc.openDataInputStream();
dos.close();
hc.close();

} catch (IOException e) {
e.printStackTrace();
}

}

public static synchronized void sendMess(String sMess) {
HttpConntSend conntSend = new HttpConntSend(sMess);
conntSend.start();

}

public HttpConntSend(String sMess) {
this.sMess = sMess;

}

}
...全文
529 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiejieguita 2009-09-10
  • 打赏
  • 举报
回复
现在不用连接两次的了。移动不会再返回资费页面了。只需要连接一次就行。发送数据时最好不要用flush方法来发送数据,直接用getResponseCode()方法就行。你的错误可能是因为flush方法造成的!
淡定别怂 2009-09-09
  • 打赏
  • 举报
回复
你这个程序在pc上能拿到数据?
楼上说的联网两次是过滤掉网关页面么?
zhlwwj1314 2009-09-09
  • 打赏
  • 举报
回复
关注
doney_dongxiang 2009-09-09
  • 打赏
  • 举报
回复
遇到过, 我记得我当时 是由于 手机使用了 wap 联网方式 中间 使用http://10.0.0.172:80/ 转发,必须手动强制 连续 连接网络两次,才能获取到数据
bzwlq 2009-09-09
  • 打赏
  • 举报
回复
各位前辈些遇到过这样的问题没?

13,100

社区成员

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

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