| « | January 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:29 评论数量:19 留言数量:0 访问次数:118464 建立时间:2006年5月29日 |

| |
|
[java与中文]乱码问题(二十) 软件技术
krf301 发表于 2007/4/21 22:48:28 |
| 我在《Java手机PDA程序设计入门》一书中看到在读取输入流的时候,为了解决正确读取字符串,建议把inputstream和inputstreamReader结合起来使用(我在本机上也的确遇到过这样的问题,读取的流里面如果含有UTF8的中文字符,用inputstream直接读,显示出来为乱码,但是用inputstreamReader创建为UTF8方式读就不会有问题)但是我在用这种方式读取163的返回时就有问题,具体代码如下:try{ current=null; current=new WaitCanvas(); Display.getDisplay(MainMidlet.instance).setCurrent(current); current.setMessage("正在连接。。。。。"); if(ConList.conmode==true){ int charpo=url.indexOf("/",7); ip=url.substring(0,charpo); String suburl=url.substring(charpo); url="http://10.0.0.172"+suburl; try{ conn=(HttpConnection)Connector.open(url,Connector.READ_WRITE,true); }catch(Exception e){ operation.FinishAlert("停止连接网络");//不连接网络时从这里抛出异常 return; } suburl=null; }else{ try{ conn=(HttpConnection)Connector.open(url,Connector.READ_WRITE,true); }catch(Exception e){ operation.FinishAlert("停止连接网络");//不连接网络时从这里抛出异常 return; } } conn.setRequestMethod(HttpConnection.POST); if(ConList.conmode==true){ conn.setRequestProperty("X-Online-Host",ip); } conn.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1"); conn.setRequestProperty("Content-Language","en-US"); conn.setRequestProperty("Content-Type","application/octet-stream"); conn.setRequestProperty("Accept","application/octet-stream"); conn.setRequestProperty("Connection","Keep-Alive");// osStrm=conn.openOutputStream(); current.setMessage("连接成功。。。。。");// DataOutputStream os=new DataOutputStream(osStrm); current.setMessage("发送请求中。。。。。");// for(int i=0;i<requestcon.length;i++){// os.writeUTF(requestcon[i]);// }// os.close(); current.setMessage("发送请求完成。。。。。"); current.setMessage("接收信息中。。。。。"); int rc=0; try{ rc=conn.getResponseCode(); }catch(Exception e){ operation.FinishAlert("无法建立连接");//成功发出request后没有收到服务器的回馈信息 return; } if(rc!=HttpConnection.HTTP_OK){ operation.FinishAlert("服务器没有回应");//成功发出request后没有收到服务器的回馈信息 return; } isStrm=conn.openInputStream(); isr=new InputStreamReader(isStrm); try{ isr=new InputStreamReader(isStrm,"UTF8"); }catch(Exception e1){ try{ isr=new InputStreamReader(isStrm,"UTF-8"); }catch(Exception e2){ try{ isr=new InputStreamReader(isStrm,"utf8"); }catch(Exception e3){ isr=new InputStreamReader(isStrm,"utf-8"); } } } int ic; StringBuffer b=new StringBuffer(); while ((ic = isr.read()) != -1) { b.append((char)ic); } content=b.toString(); isr.close(); current.setMessage("接受完成。。。。。"); }catch(IOException e){ operation.FinishAlert("连接异常");//无法连接网络时从这里抛出异常 return; }finally{ try{ if(conn!=null) conn.close(); if(isStrm!=null) isStrm.close(); }catch(Exception e){ operation.FinishAlert("关闭错误"); } } Alert a=new Alert("提示信息"); a.setString(content); a.setTimeout(Alert.FOREVER); Display.getDisplay(MainMidlet.instance).setCurrent(a);特别是中间黑体的部分,如果有这一段,读取163会提示连接异常,如果没有的话,就能读取的到信息下来,为什么在局域网中能用的方法到这些网页上就不能用了而且对于检查conn.getResponseCode()是否为ok的状态,我感觉也是有时用了这个判断会连不上,如果不用的话直接读取反而还可以读取的信息,怎么会有这样的情况 |
|
|