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
Contributor
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

garbage data problem

hie,

 

m getting some garbage like [0,0] in response with the HTTP connection please can anybody help me out.. Smiley Happy

Highlighted
Developer
Posts: 146
Registered: ‎07-24-2008
My Device: none
My Carrier: none

Re: garbage data problem

can we have code snippet.

Ankit
Thanks & Regards,
Ankit Dubey
Module Lead Software Engineer, mLabs
Impetus InfoTech (India) Pvt. Ltd.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: garbage data problem

To be sure we can be helpful, we will also need to know the connection method, the OS levels involved, whether you are seeing this on the Simulator or on device or both, devices involved etc.  Give us as much as you can please.  

Contributor
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

Re: garbage data problem

my connection code is---

 

This may help u and others while making HTTP connection..

this is ver usefull code....

 

 

thnx n regards,

Nilesh

 

private static String getConnectionString() {
     
     
        String connectionString = null;
        if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
         

            String carrierUid = getCarrierBIBSUid();
            if (carrierUid == null) {
                connectionString = ";deviceside=true";
            } else {
             
             
                connectionString = ";deviceside=false;connectionUID="
                        + carrierUid + ";ConnectionType=";
            }
        } else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
            Dialog.alert("Device is connected via Wifi.");
            connectionString = ";interface=wifi";
        }
   
        else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
            Dialog.alert("MDS coverage found");
            connectionString = ";deviceside=false";
        }
        else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
            Dialog.alert("There is no available connection.");
        }

   
        else {
            Dialog.alert("no other options found, assuming device.");
         
            connectionString = ";deviceside=true";
        }

        return connectionString;
    }

  
    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 void getHttp(String url) { // main method
        System.out.println("/// url- " + url);
 
        HttpConnection con = null;
   
        InputStream in = null;
        StringBuffer bf = new StringBuffer();
        Status.show("Please Wait....");

        try {

            try {

                con = (HttpConnection) Connector.open(url
                        + getConnectionString());
             
                con.setRequestProperty("User-Agent",
                        "Profile/MIDP-2.1 Configuration/CLDC-1.1"); // setting
                                                                    // the
               
                con.setRequestProperty("Content-Language", "en-US");
                con.setRequestProperty("Content-type", "java.lang.String");
            } catch (Exception e) {
              
                Dialog.alert("Error in connection 3..Please set the proper connection-"
                        + e);
             
                return;
            }
          
            try {
            
                in = con.openInputStream();
            } catch (Exception e) {
             
                Dialog.alert("error in connection input..!Please set the proper connection-");
                return;
            }
        

            if (HttpRequestKey == -19) { // location

                parseLatLon(in);

                return;
            }
            int ch = 0;
            int i = 0;
            while ((ch = in.read()) != -1) {
        
                bf.append((char) ch);
         
                if (bf.toString().trim().endsWith("<!")) {
                    break;
                }
                if (bf.toString().trim().endsWith("^")) {
                    i++;
                }

            }

        } catch (Exception e) {
            Dialog.alert("Error in connection Start ..Set d proper connection-"
                    + e);
            System.out.println("----------http catch------  " + e);
            e.printStackTrace();
            return;
        }

      
        Status.show("Please Wait......");

     
        processRequest(bf.toString().trim()
                .substring(0, (bf.toString().length() - 2)));
     
    }

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

Re: garbage data problem

There are some reasonable ideas in this code, but be aware that this code will only cope with exceptions unless it is run on the Event Thread, which is, of course, the wrong thing to do with blocking code.  So I can not recommend that you use this code.

 

If you are looking for sample connection code, then try the httpDemo, or this KB article:

http://supportforums.blackberry.com/t5/Java-Development/What-Is-Network-API-alternative-for-legacy-O...

or even this:

http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: garbage data problem

If you're using the newest development kit, that [0.0] shows up throughout the console, and will repeat in the same line every some number of characters.

Contributor
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

Re: garbage data problem

yeah it is happening the same.... it is repeating
Contributor
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

Re: garbage data problem

@ peter ... it will be great if u tell me what is the blocking problem in my code so that i can modify. n by tht link i didnt get anything...please share ur ideas for http connections

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

Re: garbage data problem

"it will be great if u tell me what is the blocking problem in my code "

 

I have a number of issues with your code, for example

 

a) You should use MDS first if available

b) You don't get or check the http return code

c) There is no easy interface to the calling code to return an error message

 

However the biggest issue I have with this code is that in order properly execute it must run on the Event Thread.  Network operations are by their nature, blocking.  So in order to work correctly they must run off the Event Thread.  But you have code like this:

 

} catch (Exception e) {
Dialog.alert("error in connection input..!Please set the proper connection-");

...

}

 

Dialog.alert(..) requires that the code is running on the Event Thread.

 

So if you are not running on the Event Thread, your code will fail whenever there is an issue with the connection and will not inform the user.  If it is running on the Event Thread, then it will block the Event Thread and it will be thrown out by the OS on a real device. 

 

So I would be extremely surprised if you have this code working in production applications on real devices. 

 

"please share ur ideas for http connections"

 

If you look around you will see that I have shared my ideas quite frequently.  Possibly a little too frequently! Look at the sticky Threads.  . 

Contributor
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

Re: garbage data problem

hie thnx for reply..
this code is running on device correctly..didnt gey any error message yet on real device...anywayz will try to correct it by your code..
thnx
Smiley Happy