下面是个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编码方式

解决方案 »

  1.   

    注:www.test.com应为weather.infodo.9251.com
      

  2.   

    in =new BufferedReader( connection.openInputStream());
    读一行用in.readLine();这样试试.
      

  3.   

    注:服务器端的ini文件保存的是ansi编码方式你的ini文件内容应该是键值对的形式吧是的话,那就
    1.把ini文件转为unicode格式保存
    2.用Properties类去读这个文件,用getProperty(String key) 方法得到的String不会出乱码
      

  4.   

    j2me不支持readLine
    不过问题已经解决了,谢谢大家,晕,25分白费了
      

  5.   

    先放入byte[],再转成Stringbyte[] b = null; 
    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是可以的
      

  6.   

    地址用encodeURI()方法转换过来呢??