使用cmwap方式访问网络,返回的数据是乱码?

nickleyi 2008-05-10 08:02:57
在J2ME中,使用cmwap方式通过10.0.0.172访问网络,网络连接是正常的,返回值是200。但是读取inputstream数据,返回的数据是乱码?如下;

jl°ZCache-Control max-age=0W°Cache-control no-cacheW°ZCache-Control must-revalidate(后面还有类似的数据)

使用的是Nokia 6670的手机,好像曾经做测试的过程中选择访问接入点的时候,选择了GPRS方式,返回的数据是乱码,但是后来改选成cmwap方式,数据也一直不对了。不知与这个有没有关系?

另外就是,使用之前能够成功联网和读取数据的程序(之前这个功能已经调试正常),现在做测试都不能成功,返回的都是类似的乱码?是不是移动的网关出了问题?

请问高手,是什么原因造成的?怎么解决?多谢!!!

同时,将代码附在下面:
程序如下:
Form resultsForm = new Form("Results");
mDisplay.setCurrent(resultsForm);

HttpConnection httpconnection;
InputStream inputstream;

httpconnection = null;
inputstream = null;
try {
String url = "pams/s.do?p=501&j=l&c=5114&b=5098";

httpconnection = (HttpConnection)Connector.open("http://10.0.0.172/" + url);
httpconnection.setRequestProperty("X-Onlie-Host","http://wap.beijing2008.cn");
httpconnection.setRequestMethod("GET");

httpconnection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");


resultsForm.append("Response=" + httpconnection.getResponseMessage());

if (httpconnection.getResponseCode() == 200)
{
resultsForm.append("connect success!");

// Retrieve the response.
inputstream = httpconnection.openInputStream();

long actualLength = httpconnection.getLength();
byte[] raw = new byte[(int)actualLength];
int readLength = inputstream.read(raw);

String type = httpconnection.getType();

String message = new String(raw, 0, readLength);//, "UTF-8");
resultsForm.append("type=<" + type + ">" + "\n" +
"actualLength=<" + actualLength + ">" + "\n" +
"lenght=<" + readLength + ">" + "\n" +
"msg=<" + message + ">");
}
}
catch (Exception _ex) { }

以上代码在我的Nokia 6670手机上运行返回的是乱码。使用UTF-8方式也试过了,结果还是乱码,数据不正确。

请各位帮忙,多谢了!
...全文
457 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
面包你大爷 2010-12-30
  • 打赏
  • 举报
回复
楼主,我现在也遇到跟你一样的问题了,不知道你最后是怎么解决的?
zijing660 2008-06-03
  • 打赏
  • 举报
回复
http://www.j2medev.com/bbs/dispbbs.asp?boardID=21&ID=26073&page=1

参考我的这篇帖子:

很多朋友说用WAP网关连接HTTP的时候,屏幕上会返回一堆乱码,好像是重定向那个东西什么的,其实我们只要连接两次,第一次的时候把返回的东西给丢掉,第二次连接取得的就能正常用了。以下是我自己写的一个示例代码,在NOKIA 6681上测试成功,程序里的URL是可以用的,大家在真机上是可以连上的,然后会在屏幕上显示一串字符串,由于这个地址是本人的测试地址,所以返回内容时常会有变更,不过大家看到有返回东西在屏幕上显示,不是以前看到的那种一大堆东西就行了,希望能对大家有所帮助。

如果可以,申请加精啊,哈哈。。。

import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.Connector;
//import javax.microedition.io.ContentConnection;
//import javax.microedition.io.StreamConnection;
import javax.microedition.io.HttpConnection;
import java.util.Vector;

public class loginForm extends Form implements Runnable, CommandListener {
int chose = 0;
Thread thread = new Thread(this);
InputStream is = null;
HttpConnection con;
boolean first = true; //判断是否为第一次连接
Command sendCmd = new Command("Send", Command.ITEM, 1);
boolean exit;
public loginForm() {
super("Login");
thread.start();
this.setCommandListener(this);
this.addCommand(sendCmd);
}

public void run() {

}

public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("Send")) {
sendMessage("58.211.124.139:8800", "ceshi.asp?id=10");
}

public void sendMessage(String url, String server) {
try {
//写入数据
//真机通过移动网关连接URL
if (first) { //第一次连接,什么都不做,把返回信息直接丢掉,然后关闭连接,并把FIRST置为FALSE
con = (HttpConnection) Connector.open(
"http://10.0.0.172:80/" + server);
con.setRequestProperty("X-Online-Host", url);
is = con.openInputStream();
byte b[] = this.loadData(is);
String str = new String(b, "UTF-8");
// this.append(str);
is.close();
is=null;
con.close();
con=null;
first = false;
}

// 第二次连接,并且处理返回信息,显示到屏幕上
con = (HttpConnection) Connector.open(
"http://10.0.0.172:80/" + server);
con.setRequestProperty("X-Online-Host", url);

//模拟器上直接连接URL
// con = (HttpConnection) Connector.open("http://" + url + "/" +
// server);
// String s = con.getEncoding();
// System.out.println(s);
if (first == false) {
is = con.openInputStream();
byte b[] = this.loadData(is);
String str = new String(b, "UTF-8");
this.append(str);
}
// }
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (is != null) {
is.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {}
;
}
}

public byte[] loadData(InputStream is) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] tempBuf = new byte[8];
int bytesRead = 0;
try {
while ((bytesRead = is.read(tempBuf)) != -1) {
bos.write(tempBuf, 0, bytesRead);
}
} catch (IOException e) {} finally {
try {
bos.close();
is.close();
} catch (Exception e) {
System.out.println("111111111111111111");
}
}
return bos.toByteArray();
}
}

zijing660 2008-06-02
  • 打赏
  • 举报
回复
第一次返回的是移动的收费提示页面,所以是乱码,你可以发送两次试试看,第二次返回的,就是看的懂的东西了,如果要第一次不显示的话,那么看看我帮你的代码啊行:

Form resultsForm = new Form("Results");
mDisplay.setCurrent(resultsForm);

HttpConnection httpconnection;
InputStream inputstream;
boolean firstSend=true;
httpconnection = null;
inputstream = null;
try {
String url = "pams/s.do?p=501&j=l&c=5114&b=5098";

if(firstSend){
httpconnection = (HttpConnection)Connector.open("http://10.0.0.172/" + url);
httpconnection.setRequestProperty("X-Onlie-Host","http://wap.beijing2008.cn");
firstSend=false;
}
httpconnection = (HttpConnection)Connector.open("http://10.0.0.172/" + url);
if(firstSend==false){
httpconnection.setRequestProperty("X-Onlie-Host","http://wap.beijing2008.cn");
httpconnection.setRequestMethod("GET");

httpconnection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");


resultsForm.append("Response=" + httpconnection.getResponseMessage());

if (httpconnection.getResponseCode() == 200)
{
resultsForm.append("connect success!");

// Retrieve the response.
inputstream = httpconnection.openInputStream();

long actualLength = httpconnection.getLength();
byte[] raw = new byte[(int)actualLength];
int readLength = inputstream.read(raw);

String type = httpconnection.getType();

String message = new String(raw, 0, readLength);//, "UTF-8");
resultsForm.append("type= <" + type + ">" + "\n" +
"actualLength= <" + actualLength + ">" + "\n" +
"lenght= <" + readLength + ">" + "\n" +
"msg= <" + message + ">");
}
}
catch (Exception _ex) { }
}
prok 2008-05-25
  • 打赏
  • 举报
回复
确认你的xml是utf-8编码的
nickleyi 2008-05-13
  • 打赏
  • 举报
回复
kxml解析不能成功。返回的数据还是乱码。

抓到的返回http头数据如下:

content-type:application/vnd.wap.wmlc

charset=UTF-8

content-length:254

date:Mon,12 May 2008 07:16:50 GMT

server Weblogic Server 8.1 SP3 Tue Jun 29 23:11:19 PDT 2004 404973 with CRs:

set-cookie:jid=Lnnv0p7vWR!619885163;path=/

connection:Close

proxy-connection:Close

有哪位朋友帮帮忙吗?急啊!!!
lcl0516 2008-05-12
  • 打赏
  • 举报
回复
用kmxl解析
parser.setInput(is, null); //传入null 默认utf-8或者直接parser.setInput(is, "UTF-8");


httpConnection = (HttpConnection) Connector.open(URL);
...

XmlParser parser = new KXmlParser();
is = httpConnection.openInputStream();
parser.setInput(is, null);
nickleyi 2008-05-12
  • 打赏
  • 举报
回复
已经被这个问题困了好多天了,始终没有办法。事情紧急,请各位高手指教
nickleyi 2008-05-11
  • 打赏
  • 举报
回复
各位朋友,能否将代码放到手机上,试试看是否有相同的问题?编码的方式,UTF-8的我试过了,还是乱码。
cangwu_lee 2008-05-10
  • 打赏
  • 举报
回复

中文,自己要編碼。

你看看接收到的編碼,就知道了。
prok 2008-05-10
  • 打赏
  • 举报
回复
移动拦截页面吧

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧