网络连接问题

gesanri 2009-08-26 01:23:42
有人说j2me不支持数字域名?是这样吗?下面这个程序就是把某域名下的配置文件的内容打印在控制台,报错(注:我用的wtk2.5.2,我用nokia的s40 5th模拟器竟然报空指针异常):
java.lang.IllegalArgumentException: malformed URL
at com.sun.midp.io.HttpUrl.isIPv4Address(+88)
at com.sun.midp.io.HttpUrl.parseAfterScheme(+568)
at com.sun.midp.io.HttpUrl.<init>(+36)
at com.sun.midp.io.j2me.http.Protocol.connect(+18)
。。。
程序本身没问题,我试过别的域名,可以正常打印出内容,这个域名本身也应该没错(以前这个项目做过c#版的就用这个域名,我现在做j2me版)这个问题该怎么解决?域名我又无法改变

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

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(){
StreamConnection connection = null;
InputStream in = null;
StringBuffer buffer = new StringBuffer();
try{

StringBuffer sb = new StringBuffer();
sb.append("http://weather.infodo.9251.com/getresource/getresource.aspx?");
sb.append("city=");
sb.append("北京");
sb.append("&");
sb.append("dtype=");
sb.append("");
sb.append("&");
sb.append("imei=");
sb.append("");
sb.append("&");
sb.append("softe=");
sb.append("v2.0");
sb.append("&");
sb.append("mobile=");
sb.append("");
sb.append("&");
sb.append("syse=");
sb.append("");
sb.append("&");
sb.append("username=");
sb.append("");
sb.append("&");
sb.append("email=");
sb.append("");
sb.append("&");
sb.append("settel=");
sb.append("");
sb.append("&");
sb.append("citylist=");
sb.append("北京;上海;重庆;");

connection = (StreamConnection)Connector.open(sb.toString());
in = connection.openInputStream();
int ch;
while((ch = in.read()) != -1){
if(ch != '\n'){
buffer.append((char)ch);
}
else{
String line = new String(buffer.toString());

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();
}

}
}

...全文
414 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspeden 2010-03-19
  • 打赏
  • 举报
回复
访问数字域名错误的问题解决了吗?
我发现3.0的sdk更严重
manlan123 2009-10-30
  • 打赏
  • 举报
回复
怎么我 ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("baos==="+baos);

打印出来的baos== 空啊???
manlan123 2009-10-30
  • 打赏
  • 举报
回复
是不是那亚日30 楼那段代码替换你自己写的那 int ch;
while((ch = in.read()) != -1){
if(ch != '\n'){
buffer.append((char)ch);
}
else{
String line = new String(buffer.toString());

buffer = new StringBuffer();
System.out.println(line);
}
}
替换这里啊?
gesanri 2009-08-27
  • 打赏
  • 举报
回复
哈哈,问题解决了,亚日你真是超级强悍啊
kf156 2009-08-27
  • 打赏
  • 举报
回复
byte[] b = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b1 = new byte[1024];
int i;

while ((i = is.read(b1)) != -1) {
baos.write(b1, 0, i);
}
b = baos.toByteArray();
baos.close();
baos = null;


gesanri 2009-08-27
  • 打赏
  • 举报
回复
喔是在模拟器,wtk和nokia的都试过了
你说先得到byte[],这个不好办啊,j2me又没有readLine,只能一个一个地读,我用stringbuffer可以用append一个个地加,我用数组怎么一个一个的插入呢?
kf156 2009-08-27
  • 打赏
  • 举报
回复
这和URLEncoder没关系。
你现在是在真机上操作还是模拟器?
你buffer的生成本身就有编码问题。
读网络数据流,先得到byte[]再生成字符串。
gesanri 2009-08-27
  • 打赏
  • 举报
回复
URLEncoder中是用得utf-8啊,至于
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,就是能显示英语和数字,但汉字是乱码
像你说的直接String line = new String(buffer.toString().getBytes());
我刚才也试了,依然只能显示英语和数字,汉字是乱码
kf156 2009-08-27
  • 打赏
  • 举报
回复
网络传输中文建议统一用UTF8编码
Ansi的编码你模拟器直接new String(byte[])试试
gesanri 2009-08-27
  • 打赏
  • 举报
回复
恩,我又加了一句dontNeedEncoding['/'] = true;
可以打印了,但还是昨天的情况,全部乱码或仅能显示数字和字母,我把地址打印出来是
http://www.test.com/CityData/20090826/%B1%B1%BE%A9.ini
  • 打赏
  • 举报
回复
感谢楼上的


一直在关注解决
kf156 2009-08-27
  • 打赏
  • 举报
回复
输出看看是什么?
是不是斜杠被编码了
gesanri 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 kf156 的回复:]
不是,URL编码后是%FF%FF这样的格式。你可以上网搜下,应该有人用J2ME实现。
[/Quote]
http://blog.csdn.net/zst126/archive/2009/07/21/4367703.aspx
我新建了这个URLEncoder类,完后在程序中
String city = URLEncoder.encode("http://www.test.com/CityData/20090826/北京.ini");
connection = (StreamConnection)Connector.open(city);
结果它先报java.lang.IllegalArgumentException: no ':' in URL
完后我在URLEncoder这个类中加入了dontNeedEncoding[':'] = true;这句
结果它又报java.lang.IllegalArgumentException: missing host in URL。。。 无语
doney_dongxiang 2009-08-26
  • 打赏
  • 举报
回复
看来 httpConnection 连接 数字域名 真的有问题,以前听说过,但是没有试验过
kf156 2009-08-26
  • 打赏
  • 举报
回复
不是,URL编码后是%FF%FF这样的格式。你可以上网搜下,应该有人用J2ME实现。
gesanri 2009-08-26
  • 打赏
  • 举报
回复

String city = new String("北京".getBytes(),"gb2312");
connection = (StreamConnection)Connector.open("http://www.test.com/CityData/20090826/"+city+".ini");

是这个意思吗?试过了,还是刚才那样,utf-8报错,gb2312部分乱码等等,下班了,要赶班车,辛苦亚日了,回家再看你的答复
kf156 2009-08-26
  • 打赏
  • 举报
回复
URL如果有中文,先将中文用URL编码后再访问
gesanri 2009-08-26
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 kf156 的回复:]
网络地址用URL编码下试试
[/Quote]
什么意思?
kf156 2009-08-26
  • 打赏
  • 举报
回复
网络地址用URL编码下试试
gesanri 2009-08-26
  • 打赏
  • 举报
回复
我现在是用的wtk模拟器啊,其中那个数字域名问题我是又弄了一个不含数字的局域网域名www.test.com做测试,我的程序中是这样
connection = (StreamConnection)Connector.open("http://www.test.com/CityData/20090826/北京.ini");
我描述的情况都是wtk模拟器上的,不支持ansi也不支持转换吗?我转换成各种编码都差不多试过了
加载更多回复(14)

13,100

社区成员

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

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