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: 13
Registered: ‎11-21-2010
My Device: Not Specified

http connection throwing No Stack Trace Exception

Hi,

 

My application works well on the simulator ( JDE 4.6.xx, 5.0 and 6.0). However when I try to run it on my device , 8520 using os 4.6.1, it returns 'No Stack trace'.

 

I believe, it is getting stuck on this line of code:

 

httpConn = (HttpConnection) Connector.open(URL);

 

which belongs to the following code snippet: -

 

 

 try {
         
               httpConn = (HttpConnection) Connector.open(URL, Connector.READ_WRITE, true);
               httpConn.setRequestMethod(HttpConnection.POST);
               httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
               OutputStream out = httpConn.openOutputStream();
               out.write(dataOUT.toString().getBytes());
                int status = httpConn.getResponseCode();
                System.out.println("******Response code is : "+status);
                    if (status == HttpConnection.HTTP_OK) {
                       InputStream input = httpConn.openInputStream();

 

 ..............................}

 catch (IOException e) {
                  e.printStackTrace();
               }

 

 

Can anyone please suggest???         

 

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: http connection throwing No Stack Trace Exception

'No Stack Trace' is not the exception, what is the actual exception? Also, if you want a stack trace you can catch Throwable instead of IOException.

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

Is the code getting as far as reading the response status? It would help to know which half of your code contains the line that is causing the problem.

 

What connection parameters (if any) does URL have? I suspect the problem has to do with making a connection from a real device instead of a simulator.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 92
Registered: ‎01-15-2009
My Device: Blackberry 9550

Re: http connection throwing No Stack Trace Exception

Hi. If you connect from real device you need add suffix to URL which depends on the type of connection. Try to add suffix to your URL with function like that

 

 

    public static String getConnectionSuffix( int connectionType ){
        String suffix = "";
        switch ( connectionType ) {                    
            case    TYPE_MDS://MDS
                suffix = ";deviceside=false";
                break;            
            case    TYPE_DIRECT://Direct TCP
                suffix = ";deviceside=true";
                break;            
            case   TYPE_WIFI://WIFI               
                suffix = ";interface=wifi";
                break;
        }    
        return suffix;    
    }

 

 

and add property to your request

 

  httpConn.setRequestProperty("Content-Length", "" + dataOUT.toString().getBytes().length);

 

Also you may add

 

 

System.out.println("Exception"+e);

 

 

to catch and you will see exception in console.

 

Regards, Dmitry.

 

Contributor
Posts: 13
Registered: ‎11-21-2010
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

Thanks Dmitry, Ted and Mreed.

 

I added the getconnectionsuffix function and it returns ";deviceside=true"

 

I also did add the seqrequestproperty statement.

 

The exception seems to be thrown from the statement - 

 

httpConn = (HttpConnection) Connector.open(URL);

 

My code is - 

 

 

 public void run(){
            HttpConnection httpConn;
            String content = "";
            // post data to send
            StringBuffer dataOUT = new StringBuffer();
            createPOST(dataOUT, "query", query); 
            createPOST(dataOUT, "expectResult", ""+expectResult); 
            createPOST(dataOUT, "server", SERVERNAME);
            createPOST(dataOUT, "username", USERNAME);
            createPOST(dataOUT, "password", PASSWORD);
            createPOST(dataOUT, "db", DB);
            System.out.println("****************************************************************************");
            try {
                System.out.println("I entered the try block");
               
                System.out.println(getSuffix());
                System.out.println(URL);
               httpConn = (HttpConnection) Connector.open(URL);
               System.out.println("Opened the Http conecttion");
               // send data by POST
               httpConn.setRequestMethod(HttpConnection.POST);
              // httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                httpConn.setRequestProperty("Content-Length", "" + dataOUT.toString().getBytes().length);
 OutputStream out = httpConn.openOutputStream();
               out.write(dataOUT.toString().getBytes());
               // read data
                int status = httpConn.getResponseCode();
                System.out.println("******Response code is : "+status);
                    if (status == HttpConnection.HTTP_OK) {
                       InputStream input = httpConn.openInputStream();
                       System.out.println("Getting Input Stream");
                       byte[] data = new byte[256];
                       int len = 0;
                       StringBuffer raw = new StringBuffer();
                       while (-1 != (len = input.read(data))) {
                            raw.append(new String(data, 0, len));
                             System.out.println("Reading data");
                       }
                       content = raw.toString();
                       input.close();
                       System.out.println("End of try catch block ************###########" + content);
                    } else {
                       UiApplication.getUiApplication().invokeLater(new Runnable(){
                           public void run(){
                                Dialog.alert("Connection Failure. PLease Try again");
                            }
                        });
                    }
                      httpConn.close();
              } catch (Throwable e){
 System.out.println(e);
 }
My debugger returned the folowing - 
I entered the try block
 THE CP IOS ;deviceside=true
;deviceside=true
IOException credentials do not exist
Also, after the app was launched, the debugger displayed the following ( not really aware if it is connected)
APN: 
APN username: 'null'
APN password: 'null'
Connection does not exist

 

Developer
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

HI Pavan,

 

what is the kind of connection you are using? are connecting through wi-fi of TCP-IP ?

 

from your debug messages I guess you are trying to connect thru Direct TCP-IP. If so, did you specify the APN info ?

 

what is the carrier you are using?

 

did you tried to connect thru wi-fi by giving ";interface=wifi' ?

Thanks and Regards,
PraveenGoparaju.
Contributor
Posts: 13
Registered: ‎11-21-2010
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

Hi Praveen,

 

My device seems to be using tcp-ip. Although, after a little research, my network carrier in India - vodafone, may not be supporting tcp ip.

 

At first, I did not specify any apn info, later I did specify the apn to be blackberry.net but got the same result.

 

I tried with wifi as well but it still didnt seem to work

 

I used the following code to get my connection parameters -

 

 

  public  String getSuffix(){
         String connectionParameters = "";
         if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED){
             connectionParameters = ";interface=wifi";
        }else{
            int coverageStatus = CoverageInfo.getCoverageStatus();
            ServiceRecord record = getWAP2ServiceRecord();
            if(record!=null && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT){
                connectionParameters = ";deviceSide=true;ConnectionUID="+record.getUid();}
            if((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS){
                connectionParameters = ";deviceside=false";
            } else if((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT){
                connectionParameters = ";deviceside=true";
            }
        }
        System.out.println(" THE CP IOS " +connectionParameters);
        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;
    }

 

Contributor
Posts: 13
Registered: ‎11-21-2010
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

The getWAP2ServiceRecord() returns null

Contributor
Posts: 13
Registered: ‎11-21-2010
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

Hi Guys,

 

This is now working over wifi..

 

Could you please advise, how this would work ( what connection settings / properties am i missing) w/o wifi

Contributor
Posts: 13
Registered: ‎11-21-2010
My Device: Not Specified

Re: http connection throwing No Stack Trace Exception

Hi,

 

Sorry for the constant bombardment.

 

The exception getting raised is (attached).

 

Quite sure its something to do with my network parameters , ie whether i should be using wap2.0 / tcp/ip, bis, etc

 

The exception is being raised at this statement

 

 httpConn = (HttpConnection) Connector.open(URL+getSuffix());

 

My getsuffix method is:

 

 

 public  String getSuffix(){
         String connectionParameters = "";
         if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED){
             connectionParameters = ";interface=wifi";
        }else{
            int coverageStatus = CoverageInfo.getCoverageStatus();
            ServiceRecord record = getWAP2ServiceRecord();
            if(record!=null && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT){
                connectionParameters = ";deviceSide=true;ConnectionUID="+record.getUid();}
            if((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS){
                connectionParameters = ";deviceside=false";
            } else if((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT){
                connectionParameters = ";deviceside=true";
            }
        }
        System.out.println(" THE CP IOS " +connectionParameters);
        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();
            System.out.println(cid.indexOf("wptcp"));
            System.out.println( uid.indexOf("wifi"));
            System.out.println(uid.indexOf("mms"));            
            if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1
                    && uid.indexOf("mms") == -1) {
                return records[i];
            }
        }
        return null;
    }

 

 

Thanks for helping out....

Untitled.png