Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 51
Registered: ‎05-11-2010
My Device: 8900
My Carrier: Rogers

Connection Issues

Hello,

 

I am getting reports of my app having problems connecting to the internet to verify the license.  I have searched around the fourms and read some books and I thought my below code was fool proof to connect to the internet:

 

 

 

 

String connectionParameters = getConnectionParams();
        String fullUrl = url + connectionParameters;


connection = (HttpConnection) Connector.open(fullUrl);
            connection.setRequestMethod(HttpConnection.POST);
            connection
                    .setRequestProperty(
                            HttpProtocolConstants.HEADER_CONTENT_TYPE,
                            HttpProtocolConstants.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
            connection.setRequestProperty(
                    HttpProtocolConstants.HEADER_USER_AGENT, UA);
            connection.setRequestProperty(
                    HttpProtocolConstants.HEADER_CONTENT_LANGUAGE, LANG);


setPOSTData();


responseCode = connection.getResponseCode();


//handle response code here


private String getConnectionParams () {

String connSuffix = "";
        
        if (DeviceInfo.isSimulator()) {
            connSuffix = ";deviceside=true";
        } else
        if ( (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) &&
              RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
            //System.out.println("wifi is on!");
            connSuffix=";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
                 connSuffix = "deviceside=true;ConnectionUID="+uid;
            } else {
                 connSuffix = ";deviceside=true";
            }
        }
        System.out.println("Connection suffix is " + connSuffix);
        return connSuffix;
    }

}


private void setPOSTData() throws IOException {

        // first we add all our keys to our post data object
        Enumeration keys = params.keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object val = params.get(key);
            Logger.debug("Adding " + key + ":" + val);
            postData.append(key.toString(), val.toString());
        }

        // get our bytes and the length to send to the server
        byte[] pdBytes = postData.getBytes();
        connection.setRequestProperty(
                HttpProtocolConstants.HEADER_CONTENT_LENGTH, new Integer(
                        pdBytes.length).toString());

         //Logger.debug("HTTPRequest: params: " + postData.toString());

        // write our output to the connection obj
        os = connection.openOutputStream();
        os.write(pdBytes);
        os.flush();

    }

 

 

 

Am I missing something?  This is for a post request. I don't have any problems on my phone.  The phones that are having the problems all seem to be Bold 9000 and 9700.

 

Thanks for any help, I really want to try to get this solved.  Also, how can I test the Bold 9000 or 9700 in the simulator?  I don't see it as an option.

 

Thanks.

Regular Contributor
Posts: 51
Registered: ‎05-11-2010
My Device: 8900
My Carrier: Rogers

Re: Connection Issues

I have also found 2 other versions of building the connection string:

 

 

private String getConnectionParms() {
		String connectionParameters = "";

		if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
			// Connected to a WiFi access point
			connectionParameters = ";interface=wifi";
		}
		else {
			int coverageStatus = CoverageInfo.getCoverageStatus();
			ServiceRecord record = getWAP2ServiceRecord();
			if (record != null
					//To meet with 4.3 api changed CoverageInfo.COVERAGE_DIRECT to local variable set to 1
					&& (coverageStatus & CoverageInfo.COVERAGE_CARRIER) == COVERAGE_DIRECT) {
				// Have network coverage and a WAP 2.0 service book record
				connectionParameters = ";deviceside=true;ConnectionUID="
					+ record.getUid()
					;
			}
			else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
				// Have an MDS service book and network coverage
				connectionParameters = ";deviceside=false";
			}
			else if ((coverageStatus & CoverageInfo.COVERAGE_CARRIER) == COVERAGE_DIRECT) {
				// Have network coverage but no WAP 2.0 service book record
				connectionParameters = ";deviceside=true";
			}
		}

		return connectionParameters;
	}

private ServiceRecord getWAP2ServiceRecord() {
		ServiceBook sb = ServiceBook.getSB();
		ServiceRecord[] records = sb.getRecords();
		for (int i = 0; i < records.length; i++) {
			String cid = records[i].getCid().toLowerCase();
			String uid = records[i].getUid().toLowerCase();
			if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1
					&& uid.indexOf("mms") == -1) {
				return records[i];
			}
		}
		return null;
	}

 

 

And I this one:

 

 

 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=";
            }
        }                
        
        // 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;
    }

 

 

Has anyone tried any of these?

 

Thanks.

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Connection Issues

Do you know what type of error the users are getting?

 

Just an observation: anytime you resort to ";deviceside=true", you most likely need the carrier-specific APN info. If the customer has this already in his device TCP options, your "deviceside'true" will work fine. If he does not (which is often the case), you will get some type of connection error, probably a "tunnel-failed" IO exception.

 

 

 

 

Regular Contributor
Posts: 51
Registered: ‎05-11-2010
My Device: 8900
My Carrier: Rogers

Re: Connection Issues

No, I don't know what type of errors they are getting.  So, if I don't have APN info I should not use deviceside=true?

 

How do other people deal with this.   I want to have license verification in my apps but I don't know what to do about problems like this.

Highlighted
Regular Contributor
Posts: 51
Registered: ‎05-11-2010
My Device: 8900
My Carrier: Rogers

Re: Connection Issues

If I am trying to connect through WAP 2.0 should I be doing it with deviceside=true or deviceside=false?  For example:

;deviceside=true;ConnectionUID="
                    + record.getUid()

 

or

;deviceside=false;ConnectionUID="
                    + record.getUid()

 

 

I have seen examples of both.  But from what I have read deviceside=true means TCP.  I am not sure what to do as if I update my app on the blackberry app network it counts as a credit so I want to get it right the first time.

 

Thanks for any help.

BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Connection Issues

The ConnectionUID parameter should be used for WAP 2.0 connections.  The following links should help you out.

 

Different ways to make an HTTP or socket connection

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

 

Network Diagnostic Tool

http://supportforums.blackberry.com/t5/Java-Development/Network-Diagnostic-Tool/ta-p/445964

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Regular Contributor
Posts: 51
Registered: ‎05-11-2010
My Device: 8900
My Carrier: Rogers

Re: Connection Issues

Thanks Mark,

 

I saw a link to that document in another thread you posted in here:

 

http://supportforums.blackberry.com/t5/Java-Development/Use-of-ConnectionUID/m-p/135925/highlight/tr...

 

In that thread you say you should use deviceside=true; however, the article doesn't mention this.  Is the article out of date?

 

Thanks.

BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Connection Issues

You're right, deviceside=true should also be used. Not sure how that update got missed.  I'll update the article now.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker