六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 117|回复: 0

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

[复制链接]

升级  49%

233

主题

233

主题

233

主题

进士

Rank: 4

积分
745
 楼主| 发表于 2013-1-15 08:04:25 | 显示全部楼层 |阅读模式
以下代码出自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[1000];//从内存申请空间        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();}return  BTS;}//-----------------------关键函数-----------------------------    public void pauseApp(){    }       public void destroyApp(boolean unconditional){    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表