下面是个j2me程序,本来该j2me版块问的,但我问了至今无法解决,我想编码应该是相通的,就来这试试,程序很简单,就是读取服务器端的一个ini文件,并打印出来import java.io.IOException;
import java.io.InputStream;import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;public class HttpTest extends MIDlet implements CommandListener{
private Command exit,start;
public static Display display;
private Form form; public HttpTest() {
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT,1);
start = new Command("Start",Command.EXIT,1);
form = new Form("Customer Ranking");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
} protected void startApp() throws MIDletStateChangeException {
display.setCurrent(form);
} protected void pauseApp() { } protected void destroyApp(boolean arg0) { }
public void commandAction(Command command,Displayable displayable){
if(command == exit){
destroyApp(false);
notifyDestroyed();
}
else if(command == start){
ThreadTest2 t = new ThreadTest2();
t.start();
}
}
}class ThreadTest2 extends Thread{
public void run(){
HttpConnection connection = null;
InputStream in = null;
StringBuffer buffer = new StringBuffer();
try{
String city = URLEncoder.encode("http://www.test.com/CityData/20090826/北京.ini");
System.out.println(city);
connection = (HttpConnection)Connector.open(city);
// connection = (HttpConnection)Connector.open("http://www.test.com/CityData/20090826/北京.ini");
// connection.setRequestMethod(HttpConnection.GET);
in = connection.openInputStream();
int ch;
while((ch = in.read()) != -1){
if(ch != '\n' && ch != '\r'){
buffer.append((char)ch);
}
else{
String line = new String(buffer.toString().getBytes(),"gbk");
buffer = new StringBuffer();
System.out.println(line);
}
}
}catch(IOException error){
Alert alert = new Alert("Error","Cannot connect",null,null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
HttpExample.display.setCurrent(alert);
}catch(Exception e){
e.printStackTrace();
}
}
}现在的问题是,不管我这句用什么编码
String line = new String(buffer.toString().getBytes(),"gbk");
要么打印出来是全乱码,要么英文和数字可以正常显示,要么还报错,其中utf-8报错
java.lang.RuntimeException: IOException reading reader invalid first byte 10100001
at com.sun.cldc.i18n.Helper.byteToCharArray(+228)
我试过的编码方式有gbk,gb2312,us-ascii,iso-8859-1,utf-16,utf-16be,utf-16le,都不行,不使用编码也不行,实在没辙了有人说要把url编码,我也编了,汉字也转换了,也不行
注:服务器端的ini文件保存的是ansi编码方式
import java.io.InputStream;import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;public class HttpTest extends MIDlet implements CommandListener{
private Command exit,start;
public static Display display;
private Form form; public HttpTest() {
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT,1);
start = new Command("Start",Command.EXIT,1);
form = new Form("Customer Ranking");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
} protected void startApp() throws MIDletStateChangeException {
display.setCurrent(form);
} protected void pauseApp() { } protected void destroyApp(boolean arg0) { }
public void commandAction(Command command,Displayable displayable){
if(command == exit){
destroyApp(false);
notifyDestroyed();
}
else if(command == start){
ThreadTest2 t = new ThreadTest2();
t.start();
}
}
}class ThreadTest2 extends Thread{
public void run(){
HttpConnection connection = null;
InputStream in = null;
StringBuffer buffer = new StringBuffer();
try{
String city = URLEncoder.encode("http://www.test.com/CityData/20090826/北京.ini");
System.out.println(city);
connection = (HttpConnection)Connector.open(city);
// connection = (HttpConnection)Connector.open("http://www.test.com/CityData/20090826/北京.ini");
// connection.setRequestMethod(HttpConnection.GET);
in = connection.openInputStream();
int ch;
while((ch = in.read()) != -1){
if(ch != '\n' && ch != '\r'){
buffer.append((char)ch);
}
else{
String line = new String(buffer.toString().getBytes(),"gbk");
buffer = new StringBuffer();
System.out.println(line);
}
}
}catch(IOException error){
Alert alert = new Alert("Error","Cannot connect",null,null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
HttpExample.display.setCurrent(alert);
}catch(Exception e){
e.printStackTrace();
}
}
}现在的问题是,不管我这句用什么编码
String line = new String(buffer.toString().getBytes(),"gbk");
要么打印出来是全乱码,要么英文和数字可以正常显示,要么还报错,其中utf-8报错
java.lang.RuntimeException: IOException reading reader invalid first byte 10100001
at com.sun.cldc.i18n.Helper.byteToCharArray(+228)
我试过的编码方式有gbk,gb2312,us-ascii,iso-8859-1,utf-16,utf-16be,utf-16le,都不行,不使用编码也不行,实在没辙了有人说要把url编码,我也编了,汉字也转换了,也不行
注:服务器端的ini文件保存的是ansi编码方式
解决方案 »
- 关于变量不断的赋值和滞空问题
- 急急,关于BigInteger类型数求开平方的问题
- 请问大家一般用什么软件设计数据库表,和画UML图
- 大家帮小弟看看这两个正则表达式是否正确
- 请高手帮帮我解释一下这些报文的格式!
- 欢迎来到java天地(群10704269)
- 请教一个正则表达式得问题--如何匹配一个重复单个数字多次的字符串
- 简单问题,如何把整形值变成String
- 如何在MYSQL中创建数据库
- 帮改错(三):
- delAction如何得到传过来的哦a href=del.do?username=bean.getName()>"。
- Wed, 12 Aug 2009 11:52:37 +0800 (CST)如何转换成Date
读一行用in.readLine();这样试试.
1.把ini文件转为unicode格式保存
2.用Properties类去读这个文件,用getProperty(String key) 方法得到的String不会出乱码
不过问题已经解决了,谢谢大家,晕,25分白费了
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b1 = new byte[1024];
int i; while ((i = in.read(b1)) != -1) {
baos.write(b1, 0, i);
}
b = baos.toByteArray();
String s = new String(b,"gbk");
System.out.println(s);
baos.close();
baos = null;不过不知为什么一直不能用utf-8,用它就报错,用gbk是可以的