BlackBerry app unable to establish Http Connection

2019-06-04 06:11发布

This is the code that I am using for an HTTP connection:

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条回答
ら.Afraid
2楼-- · 2019-06-04 06:53

This BlackBerry Development Guide titled "Code sample: Creating a connection over HTTP by using the first available transport" worked!!

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());
    }
}
查看更多
登录 后发表回答