(紧急)httpConnection 模拟器上可以,但se W800 真机上不可以。
当打包把程序放到真机上时,出现如下错误
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://222.191.237.30:8100/asp/build.xml ";
static final String URL = "http://222.191.237.30: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());
/*
* KXmlParser parser = new KXmlParser();
*
* parser.setInput(new InputStreamReader(httpConnection
* .openInputStream())); // parser.relaxed = true;
*
* parser.nextTag(); parser.require(XmlPullParser.START_TAG,
* null, "backslash ");
*
* while (parser.nextTag() != XmlPullParser.END_TAG) {
* System.out.println(parser.getLineNumber());
* System.out.println(parser.getName());
* System.out.println(parser.getAttributeCount());
* System.out.println(parser.getAttributeName(0));
* System.out.println(parser.getAttributeValue(0));
* System.out.println(parser.getAttributeType(0));
* System.out.println(parser.getAttributeName(1));
* System.out.println(parser.getAttributeValue(1));
* System.out.println(parser.getAttributeType(1));
* System.out.println(parser.getColumnNumber());
* System.out.println(parser.getInputEncoding());
* readStory(parser); } parser.require(XmlPullParser.END_TAG,
* null, "backslash "); parser.next();
*
* parser.require(XmlPullParser.END_DOCUMENT, null, null);
*/
} 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) {
}
}
}
}
/** Read a story and append it to the list */
void readStory(KXmlParser parser) throws IOException,
XmlPullParserException {
parser.require(XmlPullParser.START_TAG, null, "story ");
String title = null;
String desc = null;
String list1 = null;
String list2 = null;
String remark = null;
while (parser.nextTag() != XmlPullParser.END_TAG) {
parser.require(XmlPullParser.START_TAG, null, null);
String name = parser.getName();
String text = parser.nextText();
System.out.println( " < " + name + "> " + text);
if (name.equals( "date "))
title = text;
else if (name.equals( "money "))
desc = text;
else if (name.equals( "list1 "))
list1 = text;
else if (name.equals( "list2 "))
list2 = text;
else if (name.equals( "remark "))
remark = text;
parser.require(XmlPullParser.END_TAG, null, name);
}
parser.require(XmlPullParser.END_TAG, null, "story ");
if (title != null) {
String m = "money: " + desc + "\nlist1: " + list1 + "\nlist2: "
+ list2 + "\nremark: " + remark;
descriptions.addElement( " " + m);
newsList.append(title, null);
}
}
}
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) {
}
}
[解决办法]
联接方式不正确吧,真机一般是用CMWAP方式上网的