站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题网络问题,客户端老是抛出UTFDataFormatException异常

网络问题,客户端老是抛出UTFDataFormatException异常

摘要: 网络问题,客户端老是抛出UTFDataFormatException异常


一个简单的程序,客户端和服务器简单的对话.

客户端发消息"this is client",然后显示服务器发来的消息.
服务器接受到消息后,返回"this is server";

服务器端

public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
DataInputStream dis = new DataInputStream(request.getInputStream());
System.out.println(dis.readUTF());
dis.close();

DataOutputStream dos = new DataOutputStream(response.
getOutputStream());
dos.writeUTF("Hello,this server");
dos.close();
}

客户端:

private int ConnectGetLink() {
DataInputStream dis = null;
HttpConnection hc = null;
DataOutputStream dos = null;

int re = 0;
try {
hc = connectServer();
dos = hc.openDataOutputStream();
sendUTF(dos, "hello,this is client.");
dos.close();
dis = hc.openDataInputStream();
urls = receiveUTF(dis);
dis.close();
} catch (IOException ex) {
strex = ex.toString();
ex.printStackTrace();
} finally {
try {
if (hc != null) {
hc.close();
}
} catch (IOException ex2) {
ex2.printStackTrace();
}
}
return re;
}

private HttpConnection connectServer() throws IOException {
HttpConnection serverHc = null;
serverHc = (HttpConnection) Connector.open(servlet,
Connector.READ_WRITE);
serverHc.setRequestMethod(HttpConnection.POST);
serverHc.setRequestProperty("Content-Type", "application/octet-stream");
return serverHc;
}

private void sendUTF(DataOutputStream dos, String s) throws
IOException {
dos.writeUTF(s);
}

private String receiveUTF(DataInputStream dis) throws IOException {
return dis.readUTF();
}

先paint里面打印结果。
用模拟器连接正常。

在真机 3100 上不正常。
显示不了服务器发来的消息,strex显示UTFDataFormatException。
服务器方面正常,显示了hello,this is client.

请大家帮忙!!!


帮顶


你别读UTF,尝试把二进制流读出,看是否正确


用二进制流。应该是编码问题


嗯,这个问题可能会出现的

因为移动网络在转发包的时候会在你的包里加上几个字节,如果刚好破坏了你的UTF头标识,就会出现这个问题


↑返回目录
前一篇: 网络问题,在模拟器上可以连接,真机上不能连接.
后一篇: getResponseCode老是有异常