读书人

httpConnect 有关问题

发布时间: 2011-12-14 23:20:17 作者: rapoo

httpConnect 问题
当打包把程序放到真机上时,出现如下错误
javax.microedition.io.connectionnotfoundexception
url是一个公网可访问的路径。

请各位高手解决一下。

============源代码如下=================
import java.io.*;
import java.util.Vector;
import org.kxml2.io.*;
import org.xmlpull.v1.*;

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

public class Newsreader extends MIDlet implements CommandListener {

// static final String URL = "http://***.***.***.***:8100/asp/build.xml ";
static final String URL = "http://***.***.***.***:8100/asp/asptest.asp ";

// "http://www.newsforge.com/newsforge.xml ";

static final String TITLE = "NewsForge ";

Vector descriptions = new Vector();

List newsList = new List(TITLE, Choice.IMPLICIT);

TextBox textBox = new TextBox( " ", " ", 256, TextField.ANY);

Display display;

Command backCmd = new Command( "Back ", Command.BACK, 0);

Command backExit = new Command( "Exit ", Command.EXIT, 0);

class ReadThread extends Thread {

public void run() {
HttpConnection httpConnection = null;
try {


httpConnection = (HttpConnection) Connector.open(URL);
int status = httpConnection.getResponseCode();
if (status == HttpConnection.HTTP_OK) {
newsList.append( "链接成功 ", null);
descriptions.addElement( "链接成功 ");
System.out.println( "链接成功 ");
} else {
if (status == 404) {
newsList.append( "找不到该页 ", null);
descriptions.addElement( "找不到该页 ");
} else {


newsList.append( "网络故障 ", null);
descriptions.addElement( "网络故障,错误码: " + status);
}
return;
}
StringBuffer messagebuffer = new StringBuffer();
DataInputStream dis = new DataInputStream(httpConnection
.openDataInputStream());
long len; // 返回内容长度
int ch; //
len = httpConnection.getLength();
if (len != -1) {


for (int i = 0; i < len; i++) {
if ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
} else {
while ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
dis.close();

newsList.append(messagebuffer.toString(), null);
descriptions.addElement(messagebuffer.toString());


//删除了xml文件处理部分
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
descriptions.addElement(e.getMessage());
newsList.append( "CNFException ", null);
} catch (IOException e) {
e.printStackTrace();
descriptions.addElement(e.getMessage());
newsList.append( "IOException ", null);
} finally {
if (httpConnection != null) {
try {
httpConnection.close();
} catch (Exception e) {



}
}
}
}
public void startApp() {
display = Display.getDisplay(this);
try {
Class.forName( "javax.microedition.io.HttpConnection ");
} catch (Exception ex) {
ex.printStackTrace();
}
newsList.setCommandListener(this);
newsList.addCommand(backExit);
textBox.setCommandListener(this);
textBox.addCommand(backCmd);
new ReadThread().start();
display.setCurrent(newsList);
}

public void pauseApp() {
}

public void commandAction(Command c, Displayable d) {

if (c == List.SELECT_COMMAND) {


if (newsList.getSelectedIndex() > = 2)
return;
String text = (String) descriptions.elementAt(newsList
.getSelectedIndex());

if (textBox.getMaxSize() < text.length())
textBox.setMaxSize(text.length());

textBox.setString(text);
display.setCurrent(textBox);
} else if (c == backCmd)
display.setCurrent(newsList);
else if (c == backExit) {
// descriptions.removeAllElements();
destroyApp(false);
notifyDestroyed();
}
}

public void destroyApp(boolean really) {



}
}

[解决办法]
把XML写在脚本语言里当一个流输出试试
[解决办法]
网上有代码的,我以前也是从GOOGLE上找到
[解决办法]
hc=Connector.open( "http://10.0.0.172/ "+路径);
hc.setRequestProperty( "X-Online-Host ",服务器地址);

好象是这样的
[解决办法]
放在真机要用移动的网关的

在J2ME网络编程中使用CMWAP代理
  在中国移动提供的网络连接中,分为CMNET和CMWAP两种,其中CMNET可以无限制的访问互联网络,资费比较贵。CMWAP类似一个HTTP的代码,只能访问支持HTTP的应用,但是资费便宜,稳定性比较差。
  在实际的J2ME网络编程中,一般需要提供以CMWAP代理的方式连接网络,在J2ME中,连接的代码和直接连接有所不同,代码如下:
HttpConnection http = (HttpConnection)Connector.open(( "http://10.0.0.172/ "+url);
http.setRequestProperty( "X-Online-Host ",ServerName);
  例如你需要访问的地址为:http://www.test.com/login/loginServlet则上面的代码就为:
HttpConnection http = (HttpConnection)Connector.open(( "http://10.0.0.172/ " + "login/loginServlet ");
http.setRequestProperty( "X-Online-Host ", "www.test.com ");
  在实际使用过程中,只需要使用实际需要访问的地址的域名或者IP来代替ServerName,例如示例中的“www.test.com”,使用后续的地址类代替代码中的url,例如示例中的“login/loginServlet”,就可以实际的使用CMWAP代理来进行连接了。
[解决办法]
在真机网络连接的时候选 cmnet,就可以了...

要上cmnwap,参照楼上各方法...
[解决办法]
2007年5月17日之后,CMWAP需要2次连接...第一次返回计费提示页面,修改程序忽略他吧。第二次以后(第N百次都可以)才返回正确的东西。
PS:CMNET我不知道怎么创建...不过CMNET貌似不会返回计费提醒页面。
[解决办法]
To: ajax_wolf() ( )
URL= "http://218.200.249.249/usercancelservlet?fr=0&ou=http://222.*.*.*:8100/asp/asptest.asp ";
httpConnection = (HttpConnection) Connector.open(URL);
httpConnection.close();

这里代码,并没有连接取消的页面,在close前加上
httpConnection.getResponseCode();

读书人网 >J2ME开发

热点推荐