nello 发表于 2013-1-15 08:04:25

解决J2ME联网时出现的中文乱码问题

以下代码出自http://blog.csdn.net/hellogv/,引用请注明出处!
package hello;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.io.*;public class Midlet extends MIDlet{    private Display display;       public Midlet(){   display = Display.getDisplay(this);    }       public void startApp(){   try{   String uURL ="http://3g.pp.cn/phone/app/gpcx.jsp?sid=1182917775043&dirid=03311317&gpdm=600156";   StringBuffer responseMessage = new StringBuffer();   HttpConnection hc = (HttpConnection)Connector.open(uURL, Connector.READ_WRITE);      hc.setRequestMethod(HttpConnection.POST);      DataOutputStream dos = hc.openDataOutputStream();      DataInputStream dis = new DataInputStream(hc.openInputStream());      //-------------------------关键代码--------------------------------      byte []str=new byte;//从内存申请空间      dis.read(str);//把读取返回的信息保存在str中         String content= BytesToString(str);//把str转换为字符串      //-------------------------关键代码--------------------------------                Form f = new Form("HTTP Test");   f.append(content);   display.setCurrent(f);      }catch(Exception e){      }    }//-----------------------关键函数----------------------------- public String BytesToString(byte[] rec) { //从字节读取内容ByteArrayInputStream bais = new ByteArrayInputStream(rec);DataInputStream dis = new DataInputStream(bais);String BTS=null;try {BTS=new String(rec,"UTF-8");bais.close();dis.close();} catch (Exception e) {e.printStackTrace();}returnBTS;}//-----------------------关键函数-----------------------------    public void pauseApp(){    }       public void destroyApp(boolean unconditional){    }}
页: [1]
查看完整版本: 解决J2ME联网时出现的中文乱码问题