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
Developer
ahisaye
Posts: 73
Registered: ‎05-27-2009
My Device: Not Specified
Accepted Solution

Networking Class

I was wondering if anyone would be willing to take a look at thi code and let me know where I went wrong. Also do you think that storing the connection string that works in perhaps persistent storage and just using it everytime would be worthwhile.

 

I based this class off of the Network Diagnostic Tool.

 

 

public class ConnectionTypes { HttpConnection httpcon; private static ConnectionTypes INSTANCE; private ServiceRecord srWAP2; private boolean coverageTCP = false, coverageMDS = false, coverageWAP2 = false, coverageWiFi = false; private boolean succeeded = false; InputStream content; public static ConnectionTypes getInstance(){ if(INSTANCE == null){ INSTANCE = new ConnectionTypes(); } return INSTANCE; } public ConnectionTypes() { if (!CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_NONE)) { initializeTransportAvailability(); } else { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Dialog.alert("You are not connected to a network."); } }); } } public InputStream getInformation(String url) { content = null; if (coverageTCP) { succeeded = getViaHttp(url + ";deviceside=true"); } if (!succeeded && coverageMDS) { succeeded = getViaHttp(url + ";deviceside=false"); } if (!succeeded && coverageWAP2) { succeeded = getViaHttp(url + ";deviceside=true" + ";ConnectionUID=" + srWAP2.getUid()); } if (!succeeded && coverageWiFi) { succeeded = getViaHttp(url + ";interface=wifi"); } return content; } private boolean getViaHttp(String url) { try { httpcon = (HttpConnection) Connector.open(url); SocketConnectionEnhanced sock = (SocketConnectionEnhanced) httpcon; sock.setSocketOptionEx(SocketConnectionEnhanced.READ_TIMEOUT, 10000L); int response = httpcon.getResponseCode(); if (response == HttpConnection.HTTP_OK) { content = httpcon.openInputStream(); return true; } else { return false; } } catch (Exception e) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Dialog.alert("There was an error connecting. Try again later."); } }); return false; } } public void closeConnection() { try { httpcon.close(); } catch (Exception e) { e.printStackTrace(); } } private void initializeTransportAvailability() { ServiceBook sb = ServiceBook.getSB(); ServiceRecord[] records = sb.getRecords(); for (int i = 0; i < records.length; i++) { ServiceRecord myRecord = records[i]; String cid, uid; if (myRecord.isValid() && !myRecord.isDisabled()) { cid = myRecord.getCid().toLowerCase(); uid = myRecord.getUid().toLowerCase(); // Wap2.0 if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1 && uid.indexOf("mms") == -1) { srWAP2 = myRecord; } } } if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT)) { coverageTCP = true; coverageWAP2 = true; } if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)) { coverageMDS = true; } if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { coverageWiFi = true; } } }

 

 

 

Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Networking Class

You need to be a little bit more specific here. What is the problem you are having? What line number etc.
Developer
ahisaye
Posts: 73
Registered: ‎05-27-2009
My Device: Not Specified

Re: Networking Class

sorry its appending 'deviceside=true (for simulator) and while it gets a result if I paste it into my browser it doesn't return a result here.
Developer
jhw1701
Posts: 176
Registered: ‎11-06-2008
My Device: Not Specified

Re: Networking Class

URL appenders are only intended for opening connection objects. They are not meant to be used in the browser.
New Contributor
Inteist
Posts: 8
Registered: ‎03-28-2010
My Device: BlackBerry 8900

Re: Networking Class

[ Edited ]

 

There are two ways to connect in the simulator.  Either setting “;deviceside=true” and having everything work, or setting “;deviceside=false” and running the MDS simulator. 

 

The BlackBerry browser however is configured to use MDS, so the MDS Simulator is necessary to bring up web pages in the simulator.

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Networking Class

Note that it is also possible to simulate a WiFi connection (";interface=wifi") on the Simulator.