【紧急求助】HTTP通信问题,请前辈们前来解答EOFException异常

「已注销」 2010-09-19 02:42:10
在模拟器上完全可以成功的读取,装到真机上就不行了。。。
报出EOFException,请前辈们帮忙查看

再模拟器上
byte num[] = new byte[1];
num[0] = is.readByte();
num[0]=14 装到真机上 num[0]=1。。。。求助



客服端的代码


public class ExamTest extends MIDlet implements CommandListener {
public static final byte WAIT = 0;// 等待

public static final byte CONNECT = 1;// 连接中

public static final byte SUCCESS = 2;// 成功

public static final byte FAIL = 3;// 失败

int state;// 当前状态

Display display = Display.getDisplay(this);

Form form = new Form("HttpTest");

// StringBuffer sb = new StringBuffer("");

// StringItem si = new StringItem(null, sb.toString());

Command connect = new Command("联网", Command.OK, 1);

Command cls = new Command("清除网址", Command.OK, 1);

Command cancel = new Command("取消", Command.CANCEL, 1);

Command exit = new Command("退出", Command.EXIT, 1);

Command back = new Command("返回", Command.BACK, 1);

ChoiceGroup cmwapCG;// 接入方式

TextField urlTF;// 网址输入框

String url;// 网址

Http http;// 网络线程

Form page;// 页面

Alert waitPage;// 等待界面

public ExamTest() {
state = WAIT;// 等待状态
// 初始化等待界面
waitPage = new Alert("请等待", "网络连接中...", null, AlertType.INFO);
waitPage.addCommand(cancel);
waitPage.setCommandListener(this);
waitPage.setTimeout(Alert.FOREVER);

// 初始化页面
page = new Form("访问的页面");
page.addCommand(back);
page.setCommandListener(this);

// 初始化
cmwapCG = new ChoiceGroup("网络接入方式:", Choice.POPUP,
new String[] { "CMWAP" }, null);
urlTF = new TextField("请输入网址:",
"http://231599447.x6.fjjsp01.com/servlet/TestHttp", 100, TextField.URL);
form.append(cmwapCG);
form.append(urlTF);
// form.append(si);
form.addCommand(connect);
form.addCommand(cls);
// form.addCommand(cancel);
form.addCommand(exit);
form.setCommandListener(this);
}

protected void destroyApp(boolean b) {

}

protected void pauseApp() {

}

protected void startApp() {
display.setCurrent(form);
}

public void commandAction(Command c, Displayable d) {

if (c == connect) {// 联网

if (checkError())
return;
// display.setCurrent(waitPage);
http = new Http();
new Thread(http).start();

} else if (c == cancel) {// 取消
http.cancel();
http = null;
display.setCurrent(form);

} else if (c == back) {// 返回
page.deleteAll();
display.setCurrent(form);

} else if (c == cls) {// 清除数据
// cls();
urlTF.setString("");

} else if (c == exit) {// 退出
destroyApp(true);
notifyDestroyed();

}

}

// private void cls() {
// form.deleteAll();// 删除日志
// sb.delete(0, sb.length());
// si.setText(null);
// form.append(cmwapCG);
// form.append(urlTF);
// form.append(si);
// }

/**
* 判断是否正确
*
* @return
*/
private boolean checkError() {
url = urlTF.getString();
if (state == CONNECT) {
addStr("当前已有网络连接,请稍候");
return true;

} else if (url == null || url.trim().equals("")) {
addStr("url不能为空");
return true;
}

if (!url.toLowerCase().startsWith("http://")) {// 添加http://
url = "http://" + url;

}
System.out.println(url);
return false;
}

/**
* 添加文字
*
* @param str
* 要添加的文字
*/
private void addStr(String str) {
// sb.append(str + "\n");
// si.setText(sb.toString());
System.out.println(str);
}

/**
* 获取URL域名
*
* @param srcUrl
* @param domain
* true为截域名,false为截地址
* @return
*/
private String getURLhost(String srcUrl) {
int k = srcUrl.toLowerCase().indexOf("/", 7);
if (k == -1)
return srcUrl.substring(7);
return srcUrl.substring(7, k);
}

/**
*
* 字符串替换方法(字符串替换字符串)
*
* @author kf156
*
* @param text
* 文本
* @param oldStr
* 旧字符串
* @param newStr
* 新字符串
* @return
*/
public static String replace(String text, String oldStr, String newStr) {
int oldLen = oldStr.length();
int k = 0;
while (k + oldLen <= text.length()) {// 检测到字符串末尾
k = text.indexOf(oldStr, k);// 从k处查找oldStr,并返回位置
if (k == -1)// 若查不到,则跳出
return text;
// 若有,则将其替换为newStr
text = text.substring(0, k) + newStr
+ text.substring(k += oldLen, text.length());
}

return text;
}

class Http implements Runnable {

HttpConnection http;

boolean cancel;// 是否取消

public void cancel() {
cancel = true;
state = WAIT;// 等待状态
}

public void run() {
// cls();
state = CONNECT;
addStr("网络连接中...");
DataInputStream is = null;
try {
boolean cmwap = cmwapCG.isSelected(0);// 是否走CMWAP
//boolean cmwap = false;
String host = getURLhost(url);
System.out.println("host==="+host);
String _url = cmwap ? replace(url, host, "10.0.0.172:80") : url;
System.out.println("_url==="+_url);
http = (HttpConnection) Connector.open(_url, Connector.READ,
true);

if (cmwap)
http.setRequestProperty("X-Online-Host", host);

http.setRequestMethod(HttpConnection.GET);

cmwapCG.setSelectedIndex(0, true);

String contentType = http.getHeaderField("Content-Type");
// System.out.println(contentType);
addStr("Content-Type=" + contentType);

if (cancel) {// 中途结束
addStr("已取消");
return;
}

if (contentType != null
&& contentType.indexOf("text/vnd.wap.wml") != -1) {// 过滤移动资费页面
page.append("移动资费页面,过滤!");
http.close();
http = null;
http = (HttpConnection) Connector.open(_url,
Connector.READ, true);
if (cmwap)
http.setRequestProperty("X-Online-Host", host);
http.setRequestMethod(HttpConnection.GET);
contentType = http.getHeaderField("Content-Type");
addStr("Content-Type=" + contentType);
}

int code = http.getResponseCode();
addStr("HTTP Code :" + code);

if (code == 200) {// 成功

is = http.openDataInputStream();

byte[] b = new byte[1024];
int len = 0;
ExamGroup eg;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
page.append("1111");
byte num[] = new byte[1];
num[0] = is.readByte();
page.append("22222");
while ((len = is.read(b)) != -1) {
if (cancel) {// 中途结束
addStr("已取消");
return;
}
dos.write(b, 0, len);
}
page.append("3333");
byte[] data = baos.toByteArray();
eg = new ExamGroup(num,true);
page.append("num=="+num[0]);
eg.deserialize(data);
page.append("55555");
page.append(eg.getTimu()[8]);
page.append("6666");


...全文
243 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2010-09-20
  • 打赏
  • 举报
回复
这个问题困扰我3天了 我什么都是自学 您帮我一下好吗?
「已注销」 2010-09-20
  • 打赏
  • 举报
回复
我对他进行了比对就是进入了application/vnd.wap.wml页面。。过滤了几次还是进入application/vnd.wap.wml页面
「已注销」 2010-09-20
  • 打赏
  • 举报
回复
亚日,。。。。我可是基本上改的您的代码,帮我看看行吗???
我今天改了一天 现在的情况就是一直
contentType=application/vnd.wap.wml
一直进入到移动的页面里面。。过滤了三次连接后还是application/vnd.wap.wml



您能留个邮件过来吗 我把改过的代码给您发过去。
kf156 2010-09-20
  • 打赏
  • 举报
回复
先别解析数据。
直接把数据输出在模拟器和真机上,比对下看,到底得到的是什么数据。
hanshuihu 2010-09-20
  • 打赏
  • 举报
回复
public class EOFExceptionextends IOException当输入过程中意外到达文件或流的末尾时,抛出此异常。

此异常主要被数据输入流用来表明到达流的末尾。注意,其他许多输入操作返回一个特殊值表示到达流的末尾,而不是抛出异常。

我觉得 这个异常是数据流结束异常,既然模拟器好着 最好检查下环境
hanshuihu 2010-09-20
  • 打赏
  • 举报
回复
EOFException
应该是对象流结束异常吧,以前好像遇到过但是当时貌似不影响数据传输
「已注销」 2010-09-20
  • 打赏
  • 举报
回复
紧急求助
「已注销」 2010-09-19
  • 打赏
  • 举报
回复
全部代码 copy走再模拟器完全可以执行。。。浪费前辈一点时间帮我看看 谢谢了
「已注销」 2010-09-19
  • 打赏
  • 举报
回复
帖子太长分两段发了


data = null;
dos.close();
dos = null;
baos.close();
baos = null;
is.close();
is = null;

state = SUCCESS;

} else {
addStr("访问页面失败");
page.append("访问页面失败: " + code);
state = FAIL;
}
} catch (IOException e) {
addStr("联网发生异常:" + e.toString());
e.printStackTrace();
if (!cancel)
page.append("网络异常:" + e.toString());
state = FAIL;

} catch (SecurityException e) {
addStr("安全异常:" + e.toString());
e.printStackTrace();
if (!cancel)
page.append("安全异常:" + e.toString());
state = FAIL;

} catch (Exception e) {
addStr("发生异常:" + e.toString());
e.printStackTrace();
if (!cancel)
page.append("异常:" + e.toString());
state = FAIL;

} finally {

if (is != null) {// 关闭is
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}

if (http != null) {// 关闭http
try {
http.close();
} catch (IOException e) {
e.printStackTrace();
}
http = null;
}
}

if (!cancel)
display.setCurrent(page);// 显示页面
}

}
}



public class ExamGroup {
private String timu[]={};//一个长达3000字的数组
private String cue[];
private byte daan[]={
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2};
private byte total ;
public ExamGroup(int bianhao){

}

public byte[] serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(bout);

System.out.println("没错");
for(int i =0 ; i<total ; i++){
dataStream.writeUTF(timu[i]);
dataStream.writeByte(daan[i]);
}
System.out.println("serialize没错");
return bout.toByteArray();
}
public String[] getTimu() {
return timu;
}
public void setTimu(String[] timu) {
this.timu = timu;
}
public String[] getCue() {
return cue;
}
public void setCue(String[] cue) {
this.cue = cue;
}
public byte[] getDaan() {
return daan;
}
public void setDaan(byte[] daan) {
this.daan = daan;
}

public byte getTotal() {
if(timu.length>120){
total=120;
}else{
total = (byte)timu.length;
}
return total;
}

}





服务端代码


public class TestHttp extends HttpServlet {

public TestHttp() {
super();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/octet-stream");

getExamGroup(request,response,1);

}

public void getExamGroup(HttpServletRequest request, HttpServletResponse response,int bianhao ) throws IOException{
response.setContentType("application/octet-stream");
ExamGroup eg = new ExamGroup(bianhao);
OutputStream os = response.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeByte(eg.getTotal());
byte[] data = eg.serialize();
dos.write(data);
response.setContentLength(dos.size());
dos.close();
os.close();

}

}


public class ExamGroup {
private String timu[]={};//一个长达3000字的数组 数组内容省略
private String cue[];
private byte daan[]={
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2};
private byte total ;
public ExamGroup(int bianhao){

}

public byte[] serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(bout);

System.out.println("没错");
for(int i =0 ; i<total ; i++){
dataStream.writeUTF(timu[i]);
dataStream.writeByte(daan[i]);
}
System.out.println("serialize没错");
return bout.toByteArray();
}
public String[] getTimu() {
return timu;
}
public void setTimu(String[] timu) {
this.timu = timu;
}
public String[] getCue() {
return cue;
}
public void setCue(String[] cue) {
this.cue = cue;
}
public byte[] getDaan() {
return daan;
}
public void setDaan(byte[] daan) {
this.daan = daan;
}

public byte getTotal() {
if(timu.length>120){
total=120;
}else{
total = (byte)timu.length;
}
return total;
}

}




13,096

社区成员

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

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