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
Highlighted
New Developer
Posts: 6
Registered: ‎09-05-2009
My Device: Not Specified

Problem with HTTP Connection

 

Hi Guys,

 

I am having a very tough time creating a kind of simple HTTP Connection, please note that i´m targeting at 4.2.1 devices, I followed this tutorial:

 

http://www.ibm.com/developerworks/opensource/tutorials/os-blackberry2/section3.html

 

That is basically what i was trying to do, I just changed the way to load the feeds, so now we only load 1 feed at a time, and not the whole list, also, we changed the GUI to use LWUIT, everything with LWUIT is working very fine, but the HTTP connection is not working, please take a look at our code:

 

 

// // IBMRssComms.java // // MSI Services, Inc. // Frank Ableson // 973.448.0070 // fableson@msiservices.com // code free to use for any purpose, commercial or otherwise package thefeed.rss; import java.io.IOException; import java.io.InputStream; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.io.StreamConnection; import net.rim.device.api.servicebook.ServiceBook; import net.rim.device.api.servicebook.ServiceRecord; import net.rim.device.api.system.ApplicationManager; import net.rim.device.api.system.DeviceInfo; import net.rim.device.api.system.EventLogger; import net.rim.device.api.system.RadioInfo; import net.rim.device.api.ui.component.Dialog; import net.rim.device.api.xml.parsers.SAXParser; import net.rim.device.api.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import telemedellin.rss.IBMRssStorage.IBMRssFeed; //import telemedellin.HttpConnectionFactory public class IBMRssComms extends Thread { private int feedId; public IBMRssComms() { } public IBMRssComms(int feedId) { this.feedId = feedId; } public void run() { InputStream inputStream = null; HttpConnection httpConnection = null; try { // open storage IBMRssStorage rss = new IBMRssStorage(); // grab list of feeds // RecordEnumeration feedList = rss.getFeedList(); // process each // while (feedList.hasNextElement()) { // IBMRssFeed theFeed = rss.createFeed(new String(feedList // .nextRecord())); if (feedId != 0) { IBMRssFeed theFeed = rss.createFeed(rss.getRecord(feedId)); // delete any Items under this feed rss.deleteFeed(theFeed.getName(), true); /* // Pruebas con un transport diferente ServiceBook sb = ServiceBook.getSB(); ServiceRecord[] records = sb.findRecordsByCid("WPTCP"); String uid = null; for(int i=0; i < records.length; i++) { //Search through all service records to find the //valid non-Wi-Fi and non-MMS //WAP 2.0 Gateway Service Record. if (records[i].isValid() && !records[i].isDisabled()) { if (records[i].getUid() != null && records[i].getUid().length() != 0) { if ((records[i].getUid().toLowerCase().indexOf("wifi") == -1) && (records[i].getUid().toLowerCase().indexOf("mms") == -1)) { uid = records[i].getUid(); break; } } } } if (uid != null) { //open a WAP 2 connection httpConnection = (HttpConnection) Connector.open( theFeed.getUrl() + ";ConnectionUID=" + uid ); } else { //Consider another transport or alternative action. httpConnection = (HttpConnection) Connector.open( theFeed.getUrl() ); } */ /* StreamConnection s = null; s = (StreamConnection) Connector.open( theFeed.getUrl() + "WAPGatewayIP=198.228.90.228;WapGatewayPort=9201;WAPGatewayAPN=wap.comcel.com.co;TunnelAuthUsername=COMCELWAP;TunnelAuthPassword=COMCELWAP" ); //HttpConnection httpConn = (HttpConnection)s; HttpConnection httpConn = (HttpConnection)s; */ //httpConn = (HttpConnection) Connector.open( theFeed.getUrl() ); //httpConnection = (HttpConnection)Connector.open( theFeed.getUrl() + ";WAPGatewayIP=198.228.90.228;WapGatewayPort=9201;WAPGatewayAPN=wap.comcel.com.co;TunnelAuthUsername=COMCELWAP;TunnelAuthPassword=COMCELWAP" ); httpConnection = (HttpConnection) Connector.open(theFeed.getUrl() + updateConnectionSuffix() ); inputStream = httpConnection.openDataInputStream(); EventLogger.register(0xb7a77dbc30dae2a2L,"My App", EventLogger.VIEWER_STRING); EventLogger.logEvent(0xb7a77dbc30dae2a2L, "The conn used: ".getBytes() , EventLogger.INFORMATION ); // connect to feed's URL inputStream = httpConnection.openDataInputStream(); // good connection? if (httpConnection.getResponseCode() == httpConnection.HTTP_OK) { // check header field for a specific encoding String desiredEncoding = "ISO-8859-1"; // iso-8859-1 String contenttype = httpConnection .getHeaderField("Content-Type"); if (contenttype != null) { contenttype = contenttype.toUpperCase(); if (contenttype.indexOf("UTF-8") != -1) { desiredEncoding = "UTF-8"; } } InputSource is = new InputSource(inputStream); // setup Encoding to match what the web server sent us is.setEncoding(desiredEncoding); // create the factory SAXParserFactory factory = SAXParserFactory.newInstance(); // create a parser SAXParser parser = factory.newSAXParser(); // instantiate our handler IBMRssXMLHandler myHandler = new IBMRssXMLHandler(theFeed); // perform the synchronous parse parser.parse(is, myHandler); } // } // dump feeds to debug window rss.dumpFeeds(); // close storage rss.closeStore(); } } catch (IOException ioe) { System.err.println("Error 160: IO Exception !: " + ioe.getMessage()); ioe.printStackTrace(); } catch (SAXException saxe) { System.err.println("Error 163: SAX Exception !: " + saxe.getMessage()); saxe.printStackTrace(); } catch (Exception e) { System.err.println("Error 166: General Error " + e.getMessage()); e.printStackTrace(); } // notify gui that we're done! ApplicationManager.getApplicationManager().postGlobalEvent( Guid.rssdatadone, 0, 0); } /** * Define el tipo de conexion a utilizar * * @return string */ public String updateConnectionSuffix() { String connSuffix = ""; if (DeviceInfo.isSimulator()) { connSuffix = ";deviceside=true"; } 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].getUid().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 = ";ConnectionUID="+uid; } else { connSuffix = ";deviceside=true"; } } return connSuffix; }; }

 

 

 

What do you think could be causing the problem? in the device, i dont get any error, when i try to update the feed i see the network activity icons blinking, and then nothing, no errors or anything, just the same screen

 

I really appreciate your help!

 

Regards,

 

Camilo

 

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Problem with HTTP Connection

Have you tried connecting device to device debugger and see what are the activities happened and are there any exceptions thrown.

Use Search. "Accept Solution" If the problem is resolved.
New Developer
Posts: 6
Registered: ‎09-05-2009
My Device: Not Specified

Re: Problem with HTTP Connection

Hi,

 

Well, actually i havent been able to do it, currently i dont have a Blackberry device, but anyway the code seems to be fine, or what do you think?

 

Thanks again for you help!

 

Camilo

New Developer
Posts: 6
Registered: ‎09-05-2009
My Device: Not Specified

Re: Problem with HTTP Connection

By the way, i am currently developing in Netbeans, not Eclipse or JDE
Developer
Developer
Posts: 324
Registered: ‎05-26-2009
My Device: Not Specified

Re: Problem with HTTP Connection

 
hi, I noticed that in your code you are first trying to use WAP2? Is it intentional?   
  if (uid != null)
{

//open a WAP 2 connection
httpConnection = (HttpConnection) Connector.open( theFeed.getUrl() + ";ConnectionUID=" + uid );
}
else
{
//Consider another transport or alternative action.
httpConnection = (HttpConnection) Connector.open( theFeed.getUrl() );
}
New Developer
Posts: 6
Registered: ‎09-05-2009
My Device: Not Specified

Re: Problem with HTTP Connection

Hi,

 

Well, we are targeting users without BIS or BES, well, probably they do have this type of connection, but it´s also possible that the main connection will be a WAP one.

 

Do you think it's a bad idea? what do you think could be happening with this?

 

Please let me know,

 

Camilo 

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

Re: Problem with HTTP Connection

With respect to your code, the general principle of attempting to use WAP before Direct TCP is, I believe, reasonable. 

 

Can you please clarify exactly what the problem is?  I can't seem to find a specific issue, like an Exception.  Connections on a Blackberry will never 'hang', they will always throw some sort of exception if they are not working, though you might have to wait two minutes for it. 

 

Having read through your code, I am confused as to which path you are actually taking, and which connection suffix logic you are actually using.  Can you remove all the redundant code and repost giving only used code?

 

Can I confirm that this is working on the Simulator and you are having trouble on the device?  Can we also have the associated OS Levels for the development IDE and the Device involved.