3

我在我的 RIM Blackberry 应用程序中执行 HttpConnection。我正在使用无线网络连接。在执行 HttpConnection 时,有时它会返回数据,有时它会给出

java.io.IOException 收音机已关闭

java.io.IOException 内存不足

错误。我真的不明白到底是什么问题。我在这里发布我的代码片段:

public static String getRemoteData(String url) throws ConnectionNotFoundException{
    StringBuffer stringBuff=new StringBuffer();     
    try {   
        HttpConnection fconImg = (HttpConnection) Connector.open(url+ NetworkUtils.getConnectionString());
        InputStream input = fconImg.openInputStream();          
        ByteArrayOutputStream baos = new ByteArrayOutputStream();           
        int k = 0;
        while ((k = input.read()) != -1) {
            baos.write(k);
        }           
        byte[] byteArray = baos.toByteArray();          
        String s = new String(byteArray);           
        stringBuff.append(s.trim());
        return stringBuff.toString();

    } catch (Exception e) {
        stringBuff.append("Exception : "+e.toString());
        return stringBuff.toString();
    }
}

下面是我的 NetworkUtils 类。

import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;

public class NetworkUtils {
    private static final String COVERAGE_CARRIER = "Carrier full Coverage";
    private static final String COVERAGE_MDS = "BES coverage";
    private static final String COVERAGE_NONE = "No coverage";
    private static final String NOT_SUPPORTED_WAF = "Not supported by the device";

    public static String logM;
    /**
     * Access the net.rim.device.api.system.DeviceInfo class in order to 
     * understand if the running system is a simulator or a real device.
     * @return true if the current application is running on a Blackberry 
     * simulator, false otherwise
     */
    public static boolean isSimulator() {
            return DeviceInfo.isSimulator();
    }

    /**
     * Give the information about the presence o a wifi bearer on the device
     * @return true if the wifi communication interface bearer is supported by
     * the device, false otherwise
     */
    protected static boolean isWifiAvailable() {
            //  Log.info("Checking WIFI Availability");
            boolean isWifiEnabled;
            if (RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
                    //   Log.info("WIFI Supported");
                    isWifiEnabled = true;
            } else {
                    //   Log.info("WIFI NOT Supported");
                    isWifiEnabled = false;
            }
            return isWifiEnabled;
    }

    /**
     * Give information about the presence of active wifi connections. 
     * @return true if the device is connected to a wifi network with its wifi 
     * bearer, false otherwise
     */
    protected static boolean isWifiActive() {

            int active = RadioInfo.getActiveWAFs();
            int wifi = RadioInfo.WAF_WLAN;
            return active >= wifi;
    }

    protected static boolean isWapGprsDataBearerOffline() {
            return RadioInfo.getState()==RadioInfo.STATE_OFF ||
            RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE;
    }

    public static String getNetworkCoverageReport() {
            StringBuffer sb = new StringBuffer();

            sb.append("\n*********************************************************");
            sb.append("\nWireless Access Families:");
            sb.append("\n3GPP: " + getNetworkCoverage(RadioInfo.WAF_3GPP));
            sb.append("\nCDMA: " + getNetworkCoverage(RadioInfo.WAF_CDMA));
            sb.append("\nWLAN: " + getNetworkCoverage(RadioInfo.WAF_WLAN));
            sb.append("\nCDMA: " + getNetworkCoverage(RadioInfo.NETWORK_CDMA));
            sb.append("\nBands:");
            sb.append("\nCDMA_800: " + getNetworkCoverage(RadioInfo.BAND_CDMA_800));
            sb.append("\nCDMA_1900: " + getNetworkCoverage(RadioInfo.BAND_CDMA_1900));
            sb.append("\nNetworks:");
            sb.append("\n802_11: " + getNetworkCoverage(RadioInfo.NETWORK_802_11));
            sb.append("\nGPRS: " + getNetworkCoverage(RadioInfo.NETWORK_GPRS));
            sb.append("\nNetwork services:");
            sb.append("\nVOICE: " + getNetworkCoverage(RadioInfo.NETWORK_SERVICE_VOICE));
            sb.append("\nUMTS: " + getNetworkCoverage(RadioInfo.NETWORK_SERVICE_UMTS));
            sb.append("\nEDGE: " + getNetworkCoverage(RadioInfo.NETWORK_SERVICE_EDGE));
            sb.append("\n*********************************************************");
            return sb.toString();
    }

    private static String getNetworkCoverage(int networkType) {
            if (RadioInfo.areWAFsSupported(networkType)) {
                    int status = CoverageInfo.getCoverageStatus(networkType, false);
                    switch (status) {
                    //  case CoverageInfo.COVERAGE_DIRECT: //TODO if we switch back to < 4.5 we must use CARRIER
                    //      return COVERAGE_CARRIER;//not support less ver of 4.5 
                    case CoverageInfo.COVERAGE_MDS:
                            return COVERAGE_MDS;
                    case CoverageInfo.COVERAGE_NONE:
                            return COVERAGE_NONE;
                    default:
                            break;
                    }
            } 
            return NOT_SUPPORTED_WAF;
    }

    public static boolean isDataConnectionAvailable() {
            boolean ret = (isWifiAvailable()&&isWifiActive())||!isWapGprsDataBearerOffline();
            return ret;
    }

    /**
    * Determines what connection type to use and returns the necessary string to use it.
    * @return A string with the connection info
    */
    public static String getSubURL()
    {
        // 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())
        {
                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";
        }else{
            String uid = null;
            ServiceBook sb = ServiceBook.getSB();
            ServiceRecord[] records = sb.findRecordsByCid("WPTCP");
            for (int i = 0; i < records.length; i++) {
                if (records[i].isValid() && !records[i].isDisabled()) {
                    if (records[i].getUid() != null &&
                        records[i].getUid().length() != 0) {
                        if ((records[i].getCid().toLowerCase().indexOf("wptcp") != -1) &&
                            (records[i].getUid().toLowerCase().indexOf("wifi") == -1) &&
                            (records[i].getUid().toLowerCase().indexOf("mms") == -1)   ) {
                            uid = records[i].getUid();
                            break;
                        }
                    }
                }
            }
            if (uid != null) {
                // WAP2 Connection
                connectionString = ";ConnectionUID="+uid;
            } else {
                connectionString = ";deviceside=true";
            }
        }   
    return connectionString + ";ConnectionTimeout=60000";
    }

    /**
    * 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;
    }

    public static void logMessage(String str)
    {
        logM=str;   
    }

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

}

首先十分感谢....

4

1 回答 1

1

我不知道这是否是问题所在,但您在调用 .toByteArray() 之前没有关闭 ByteArrayOutputStream (调用 baos.close() )

于 2013-01-29T17:41:10.680 回答