BlackBerry app无法与服务器建立Http Connection

时间:2010-08-20 12:44:37

标签: http connection blackberry-simulator blackberry-eclipse-plugin

这是我用于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();

1 个答案:

答案 0 :(得分:1)

此黑莓开发指南标题为“Code sample: Creating a connection over HTTP by using the first available transport”工作!!

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