【问题标题】:Blackberry HttpConnection failure on device设备上的黑莓 HttpConnection 失败
【发布时间】:2012-03-20 22:05:01
【问题描述】:

我再次寻求一些 BlackBerry 建议。我正在使用附加到 URI 连接字符串的标准 BB 代码开发一个基于 REST 的应用程序(如果您愿意,我会发布但不想在这里占用空间,因为我怀疑知道这方面的人知道正是我的意思)。

该代码在 MDS 模式下的模拟器中运行良好,并且在手机上使用直接 WiFi 也很好。

现在,问题是当我开始在实际手机上使用 3G 时。到那时它就失败了。这是某种转码问题吗?

我正在使用原始的 HttpConnection。

HTTP POST 有效(带有正文信息),但 GET(使用 cookie 进行身份验证作为标头请求属性)失败。

故障仅在于移动设备上基于标头 (GET) 的非 WiFi 连接信息。

任何建议将不胜感激。

public static String httpGet(Hashtable params, String uriIn) {

        String result = null;

        LoginDetails loginDetails = LoginDetails.getInstance();

        HttpConnection _connection;

        String uri = uriIn + "?api_key=" + loginDetails.getApi_key();

        Enumeration e = params.keys();

        // iterate through Hashtable keys Enumeration
        while (e.hasMoreElements()) {
            String key = (String) (e.nextElement());
            String value = (String) params.get(key);

            uri += "&" + key + "=" + value;

        }

        uri = uri + HelperMethods.getConnectionString();

        try {

            _connection = (HttpConnection) Connector.open(uri);

            _connection.setRequestMethod(HttpConnection.GET);
            _connection.setRequestProperty("Content-Type",
                    "text/plain; charset=UTF-8");

            _connection.setRequestProperty("x-rim-authentication-passthrough",
                    "true");

            _connection.setRequestProperty("Cookie", loginDetails.getCookie());

            _connection.setRequestProperty("Content-Type", "application/json");

            String charset = "UTF-8";

            _connection.setRequestProperty("Accept-Charset", charset);
            _connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=" + charset);

            OutputStream _outputStream = _connection.openOutputStream();

            int rc = _connection.getResponseCode();

            InputStream _inputStream = _connection.openInputStream();

            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = _inputStream.read()) != -1) {

                bytestream.write(ch);

            }

            result = new String(bytestream.toByteArray());

            bytestream.close();


            {

                if (_outputStream != null)
                    try {
                        _outputStream.close();
                    } catch (Exception e1) {
                    }
                if (_connection != null)
                    try {
                        _connection.close();
                    } catch (Exception e2) {
                    }

            }

        } catch (IOException e3) {

            // TODO Auto-generated catch block
            e3.printStackTrace();
        }

        return result;

    }

这个用途:

public synchronized static String getConnectionString() {

        String connectionString = null;

        // Simulator behaviour is controlled by the USE_MDS_IN_SIMULATOR
        // variable.
        if (DeviceInfo.isSimulator()) {

            connectionString = ";deviceside=true";
        }

        // Wifi is the preferred transmission method
        else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {

            connectionString = ";interface=wifi";
        }

        // Is the carrier network the only way to connect?
        else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {

            String carrierUid = getCarrierBIBSUid();

            if (carrierUid == null) {
                // Has carrier coverage, but not BIBS. So use the carrier's TCP
                // network

                connectionString = ";deviceside=true";
            } else {
                // otherwise, use the Uid to construct a valid carrier BIBS
                // request

                connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
            }
        }

        // Check for an MDS connection instead (BlackBerry Enterprise Server)
        else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {

            connectionString = ";deviceside=false";
        }

        // If there is no connection available abort to avoid hassling the user
        // unnecssarily.
        else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
            connectionString = "none";

        }

        // In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ...
        else {

            connectionString = ";deviceside=true";
        }



        return connectionString;
    }

    /**
     * Looks through the phone's service book for a carrier provided BIBS
     * network
     * 
     * @return The uid used to connect to that network.
     */
    private synchronized static String getCarrierBIBSUid() {
        ServiceRecord[] records = ServiceBook.getSB().getRecords();
        int currentRecord;

        for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
            if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
                if (records[currentRecord].getName().toLowerCase()
                        .indexOf("bibs") >= 0) {
                    return records[currentRecord].getUid();
                }
            }
        }

        return null;
    }

【问题讨论】:

  • 您实际上遇到了什么样的失败?请显示实际错误。另外,为什么在发送请求之前将请求Content-Type 多次设置为不同的值?您不能在 GET 请求中发送任何正文数据,因此即使设置 Content-Type 也没有任何意义,因为没有内容。 URL 的字符集由服务器决定,而不是 HTTP 标头。
  • 为测试设置了多次。没有一个真正在设备上工作。
  • 为什么这可以在设备上的 WiFi 而不是其他服务上工作?
  • 现在同样适用,我已经删除了 Content-Type 等。这没有任何区别。 POST 请求通过所有方式工作,但上面的 GET 除非在设备上使用 WiFi。
  • 你还没有说实际的问题是什么。只是说它不起作用并不是有用的信息。 什么 不能正常工作? WHAT 到底失败了?在哪条线上?是否抛出异常?有报错信息吗?

标签: http blackberry get connection device


【解决方案1】:

已修复 - 见上文。

原来 uri 中有空格。

为什么这在 WiFi 而不是 3G 等上有效仍然令人费解。

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2012-01-27
    • 2011-09-04
    • 1970-01-01
    • 2011-01-24
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多