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: 23
Registered: ‎03-10-2010
My Device: 8530
My Carrier: student

Internet

 

Hi, I have an application that should view a web page in that,the function is:

 


     public  String getPage(String url) {
        
             String response = "";
             boolean htmltag = false;
             StringBuffer newhtml = new StringBuffer();
             boolean istag = false;


         try {
                  StreamConnection s = (StreamConnection)Connector.open(url);
                  InputStream input = s.openInputStream();
                  
                   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));
                  }

                    response = raw.toString();
                  
                        input.close();
                        s.close();
             } catch(Exception e) { }

              return response;
       }

 

but doesn't work,can I have to use browser for that!!!?

 

 

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Internet

i would suggest that you use a httpconnection. don't forget to execute networking on a separate thread.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 23
Registered: ‎03-10-2010
My Device: 8530
My Carrier: student

Re: Internet

Hi,thanks I'm downloading JDE v5.0 for the classes ,I think I have to create a Threat and un Http connection before :-)

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Internet

Basically turn this...

 

 

try 
{
  StreamConnection s = (StreamConnection)Connector.open (url);
  InputStream input = s.openInputStream ();
  
  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));
  }
  
  response = raw.toString();
  
  input.close();
  s.close();
} 
catch (Exception e) 
{ }

 

 

into this...

 

 

try 
{
  new Thread ( public void run () 
               {
                   HttpConnection s = (HttpConnection)Connector.open (url);
                   InputStream input = s.openInputStream ();
                   
                   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));
                   }
                   
                   response = raw.toString();
                   
                   input.close();
                   s.close();
               }).start ();
} 
catch (Exception e) 
{ }

 

 

I *think* that should work.

 

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Contributor
Posts: 23
Registered: ‎03-10-2010
My Device: 8530
My Carrier: student

Re: Internet

Thanks

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

Re: Internet

The code in the httpdemo also provides a good sample for doing this sort of thing.

 

Note that in Dom's code, you will need a try catch inside the Thread run method. 

Contributor
Posts: 23
Registered: ‎03-10-2010
My Device: 8530
My Carrier: student

Re: Internet

Nel mio caso ho fatto una classe del genere e funziona

 

class ConnectionThread extends Thread
{
    String url;
    String responseMessage = "";
   
 
   
    ConnectionThread(String Url){
        
        this.url=Url;
        
        }
    
 
 public void run()
    {
         
          ConnectionFactory connFact = new ConnectionFactory();
          ConnectionDescriptor connDesc;
          connDesc = connFact.getConnection(url);
          
          InputStream inputStream;
         

            if (connDesc != null)
          {
              HttpConnection httpConn;
              httpConn = (HttpConnection)connDesc.getConnection();
              
             try
              {
                   
                  inputStream = httpConn.openInputStream();
                  if (inputStream != null) {
                    InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
                    /*
                     * Getting the number of bytes that can be read from the
                     * input stream
                     */
                    int readCharacter;
               
                    StringBuffer responseBuffer = new StringBuffer();
                    while ((readCharacter = reader.read()) != -1) {
                        responseBuffer.append((char) readCharacter);
                    }
                    /* Converting the received bytes to string */
                    responseMessage = new String(responseBuffer);
                 }
                    
                   
               //  final int iResponseCode = httpConn.getResponseCode();
                  UiApplication.getUiApplication().invokeLater(new Runnable()
                  {
                      public void run()
                      {
                          Dialog.alert("Response code: " +responseMessage);
                                  
                      }
                   });
               }
               catch (IOException e)
               {
                 System.err.println("Caught IOException: "
                      + e.getMessage());
               }
                     
          }
    }

}

poi il thread lho fatto partire da un altra parte

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Internet

I don't mean to sound disrepectful, but can you say that in English?

----------------------------------------------------------------------------
chown -R us ./base
~J!NX