Включение нескольких типов подключения - PullRequest
1 голос
/ 25 марта 2012

В моем приложении есть HTTP-соединение, в настоящее время оно применимо только в том случае, если пользователи подключены к WIFI-соединению.Как бы я это сделал, если бы хотел включить его для любых подключений, которые имеет конкретное устройство?Скажите, что на устройстве есть служба BIS или даже обычная служба WAP.

Это мой код.

try 
    {
        connection = (HttpConnection) Connector.open(url+ ";interface=wifi");
        is = connection.openInputStream();  
        try 
        {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.parse(is, rssHandler);
        } 
        catch (ParserConfigurationException e) 
        {
            e.printStackTrace();
        } 
        catch (SAXException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    } 

    catch (IOException e) 
    {
        e.printStackTrace();
        Dialog.inform("Bad URL");
    } 

1 Ответ

3 голосов
/ 26 марта 2012

Используйте следующий код для получения строки подключения

/**
 * Determines what connection type to use and returns the necessary string to use it.
 * @return A string with the connection info
 */
private static String getConnectionString()
{
    // This code is based on the connection code developed by Mike Nelson of AccelGolf.
    // http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection
    String connectionString = null;

    // Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
    if(DeviceInfo.isSimulator())
    {
            if(UploaderThread.USE_MDS_IN_SIMULATOR)
            {
                    logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is true");
                    connectionString = ";deviceside=false";
            }
            else
            {
                    logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
                    connectionString = ";deviceside=true";
            }
    }

    // Wifi is the preferred transmission method
    else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
    {
        logMessage("Device is connected via Wifi.");
        connectionString = ";interface=wifi";
    }

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

        String carrierUid = getCarrierBIBSUid();
        if(carrierUid == null)
        {
            // Has carrier coverage, but not BIBS.  So use the carrier's TCP network
            logMessage("No Uid");
            connectionString = ";deviceside=true";
        }
        else
        {
            // otherwise, use the Uid to construct a valid carrier BIBS request
            logMessage("uid is: " + carrierUid);
            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)
    {
        logMessage("MDS coverage found");
        connectionString = ";deviceside=false";
    }

    // If there is no connection available abort to avoid bugging the user unnecssarily.
    else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
    {
        logMessage("There is no available connection.");
    }

    // In theory, all bases are covered so this shouldn't be reachable.
    else
    {
        logMessage("no other options found, assuming device.");
        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 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...