【发布时间】:2011-12-08 13:07:39
【问题描述】:
我正在尝试从我的黑莓模拟器测试 http 连接,但没有任何反应。我需要一个 MDS 模拟器来运行这个应用程序吗? 我正在使用 Eclipse Helios BlackBerry Java 插件 1.5.0。我还在我的系统中安装了黑莓 JDE。
void getViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
int rc;
try {
c = (HttpConnection)Connector.open(url);
RichTextField rt = new RichTextField("TEST LENGTH");
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
// Get the ContentType
String type = c.getType();
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
rt = new RichTextField("TEST LENGTH"+len);
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
} else {
rt = new RichTextField("GREATER LENGTH"+len);
int ch;
while ((ch = is.read()) != -1) {
}
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
}
非常感谢您的帮助, 谢谢, VKS
【问题讨论】:
-
出于好奇,您如何实际检测这是否成功?从外观上看,没有任何东西可以表明您实际上得到了字符串。如果它没有抛出任何异常,那么它很可能正在工作。
-
但我在 3 个条件下添加了 3 个 RichTextField,在屏幕中找不到?
-
您正在创建新的
RichTextFields,但实际上并未将它们添加到屏幕上。
标签: java blackberry blackberry-jde