|
查看: 1614|回复: 7
|
Midlet 显示Text File,中文出现乱码..
[复制链接]
|
|
|
如题。以下是我的Code.当Text File 里有中文字就会出现乱码,请帮忙。
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HttpTXT extends MIDlet implements CommandListener {
private Command exitCommand, loadCommand;
private Form mainForm;
private TextField tfItem;
private TextField tf;
public HttpTXT() {
mainForm = new Form("Download and Display Text");
tf = new TextField("Text URL", "http://127.0.0.1:8080/TEST.txt", 120,
TextField.URL);
tf.setLayout(Item.LAYOUT_NEWLINE_AFTER);
tfItem = new TextField("Text", "", 500, TextField.ANY);
tfItem.setPreferredSize(mainForm.getWidth(), 200);
tfItem.setLayout(Item.LAYOUT_VEXPAND | Item.LAYOUT_2);
mainForm.append(tf);
mainForm.append(tfItem);
exitCommand = new Command("Exit", Command.EXIT, 1);
loadCommand = new Command("Dowload", Command.SCREEN, 1);
mainForm.addCommand(exitCommand);
mainForm.addCommand(loadCommand);
mainForm.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(mainForm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean p1) {
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == loadCommand) {
new DownLoadTXT(mainForm, tfItem, tf.getString());
}
}
private class DownLoadTXT extends Thread {
private String txtURL;
private Form form;
private TextField tfItem;
public DownLoadTXT(Form f, TextField i, String url) {
form = f;
tfItem = i;
txtURL = url;
this.start();
}
public void run() {
String str = null;
try {
str = getText(txtURL);
} catch (Exception e) {
System.out.println("Error " + e.getMessage());
}
form.setTicker(null);
if (tfItem != null)
tfItem.setString(str);
}
public String getText(String url) throws IOException, Exception {
HttpConnection c = null;
InputStream is = null;
String str =null;
try {
c = (HttpConnection) Connector.open(url);
int rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new Exception(c.getResponseMessage());
}
is = c.openInputStream();
int length = (int) c.getLength();
if (length > 0) {
byte servletData[] = new byte[length];
is.read(servletData);
str = new String(servletData);
}
} catch (IOException e) {
System.out.println("error" + e.getMessage());
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
return str;
} }} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 17-1-2010 09:18 PM
|
显示全部楼层
除此之外,platformRequest("http://.../AutoUpdate.wml");如果里面有中文字也是出项乱码。我的wml file 如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
<anchor>
This is the latest version。尝试
<go href="http://.../abc.jar">
</go>
</anchor>
</p>
</card>
</wml> |
|
|
|
|
|
|
|
|
|
|
发表于 17-1-2010 11:07 PM
|
显示全部楼层
本帖最后由 兔仙人 于 17-1-2010 11:10 PM 编辑
<DocumentOptions>
<CharSet>UTF-8</CharSet>
</DocumentOptions>
试试 加 这个 进去 。。 |
|
|
|
|
|
|
|
|
|
|
发表于 18-1-2010 01:07 AM
|
显示全部楼层
header(″Content-type: text/vnd.wap.wml; charset=gb2312″);
或者试试这个 。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-1-2010 01:17 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-1-2010 01:36 PM
|
显示全部楼层
|
wml 解决了。不过Text file的还是不可以. |
|
|
|
|
|
|
|
|
|
|
发表于 18-1-2010 05:49 PM
|
显示全部楼层
|
JAVA 那边 , 你 可以试试 ,找找 关于 INTERNATIONALIZATION 和 LOCALIZATION 的 资料 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 20-1-2010 10:58 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|