点击下面标题查看本文完整版:J2ME的HTTP连接,服务器出现EOFException
问题是这样的,J2ME的HTTP连接,服务器出现EOFException,模拟器上联服务器 一点问题都没有, 可是换上真机后
程序启动:第一次联,无错
不关闭程序 第二次联,首先出现 EOFException,问题好像出在readUTF()上,但是马上会有第二次连接,返回结果到客户端
不关闭程序,同时把发送出去的字符串加长,则出现EOFException,而且不会再有连接出现,程序就死那里了
客户端联网部分如下
private void connect(String url) throws IOException {
HttpConnection httpConn = null;
DataOutputStream dos = null;
DataInputStream dis = null;
String s = null;
String s1 = null;
try {
System.out.println("connection to server...");
//httpConn = (HttpConnection)Connector.open(url);
//如果用模拟器,将此处向下注释,上行取消注释
?
httpConn = (HttpConnection)Connector.open("http://10.0.0.172/"+url);
httpConn.setRequestProperty("X-Online-Host","sina.cn");
s1 = httpConn.getHeaderField("Content-Type"); ?
//资费页
if(s1.startsWith("text")){
//重新发起一次请求
httpConn = (HttpConnection)Connector.open("http://10.0.0.172/"+url);
httpConn.setRequestProperty("X-Online-Host", "sina.cn");
//s1 = "send again";
}
//?
//用模拟器注释到此
System.out.println("OK");
// 传送数据
httpConn.setRequestMethod(HttpConnection.POST);
System.out.println("good");
dos = new DataOutputStream(httpConn.openDataOutputStream());
System.out.println("write");
dos.writeUTF(userName);
? dos.writeUTF(password);
System.out.println("write");
//dos.flush();
// 接收servlet判断的数据
dis = new DataInputStream(httpConn.openDataInputStream());
String returnStr = dis.readUTF();
//int returnStr = dis.readInt();
//s = new String("returnStr:" + returnStr + " " + httpConn.getLength() + " " + httpConn.getURL().getBytes().length);
s = new String("returnStr:" + returnStr);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
if (httpConn != null)
httpConn.close();
}
// 显示返回的结果
midlet.endMIDlet(s);
}
服务器端如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = "";
String password = "";
System.out.println("Connecting Server.....");
DataInputStream dis = null;
dis = new DataInputStream(request.getInputStream());
try{
username = dis.readUTF();
password = dis.readUTF();
}catch(Exception e) {
e.printStackTrace();
}
finally{
if (dis != null){
dis.close();?
dis = null;
}
}
DataOutputStream dos = new DataOutputStream(response.getOutputStream());
try{
String returnStatus = "-1";
System.out.println(returnStatus);
dos.writeUTF(returnStatus);
}catch(Exception e) {
e.printStackTrace();
}
finally{
if (dos != null){
dos.close();
dos = null;
}
}
}
请大虾们赐教,谢谢
------解决方法--------------------------------------------------------
要确定下具体出错的代码位置才好分析
------解决方法--------------------------------------------------------
自己先确定一下究竟是否是移动资费页面的问题,有时候返回了移动资费页面而你去readUTF的话就会出现你这样的问题.