这是我用于HTTP连接的代码:

HttpConnection connection = null;
// InputStream inputstream = null;
connection = (HttpConnection) Connector.open("http://www.google.com");
//HTTP Request
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("Content-Type","//text plain");
connection.setRequestProperty("Connection", "close");
add(new LabelField(""+connection.getResponseMessage()));
connection.close();
有帮助吗?

解决方案

该黑莓开发指南标题为“代码示例:通过使用第一个可用传输通过HTTP创建连接“工作了!!

ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection("http://www.google.com");
if (connDesc != null) {
    HttpConnection httpConn;
    httpConn = (HttpConnection)connDesc.getConnection();
    try { 
        final int iResponseCode = httpConn.getResponseCode();
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.alert("Response code: " + 
                    Integer.toString(iResponseCode));
            }
        });
    } catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top