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
Regular Contributor
Posts: 51
Registered: ‎01-13-2011
My Device: 9800
My Carrier: vodaphone

Re: Retrieve content from browserfield

I want to get Html after the page load in my browserfield i already added listener.

 

 

 

Vishal Lakhmapurkar
Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

I came down this road because I could not get the httpconnect classes to work.  They throw various errors including redirect problems and 302 errors etc. etc.  At least with this class I get to the content itself.  With the direct http connect classes I never even get through to the server !

 

 

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

BTW what is the httpGet class ?  I don't see that in the documentation ?

Developer
Posts: 28
Registered: ‎10-05-2009
My Device: Not Specified

Re: Retrieve content from browserfield

[ Edited ]

I used this think I might have messed about with it a bit.....

 

package com.;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;


public class HttpRequestDispatcher extends Thread
{
private String url;
private String method; //GET or POST
private FacebookPost screen;
private byte[] postData;

public HttpRequestDispatcher(String url, String method, FacebookPost networkingMainScreen, byte[] postData)
{
this.url = url;
this.method = method;
this.screen = networkingMainScreen;
this.postData = postData;
}

public void run()
{
try
{
CoverageTest ct = new CoverageTest();
if (ct.CoverageTest() != "")
{
HttpConnection connection = (HttpConnection)Connector.open(url+ct.CoverageTest());
connection.setRequestMethod(method);

if(method.equals("POST") && postData != null)
{
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
OutputStream requestOutput = connection.openOutputStream();
requestOutput.write(postData);
requestOutput.close();
}

int responseCode = connection.getResponseCode();
if(responseCode != HttpConnection.HTTP_OK)
{
screen.requestFailed("Unexpected response code: "+responseCode);
connection.close();
return;
}

String contentType = connection.getHeaderField("Content-type");
ByteArrayOutputStream baos = new ByteArrayOutputStream();

InputStream responseData = connection.openInputStream();
byte[] buffer = new byte[10000];
int bytesRead = responseData.read(buffer);

while(bytesRead >0)
{
baos.write(buffer, 0, bytesRead);
bytesRead = responseData.read(buffer);
}

baos.close();
connection.close();

screen.requestSucceded(baos.toByteArray(), contentType);
}
else
{
screen.requestFailed("No Connection");
}

}
catch(IOException e)
{
screen.requestFailed(e.toString());
}
}
}

 

And I call it with this.....

 

HttpRequestDispatcher url = new HttpRequestDispatcher(url, "GET", this, null);
url.start();

 With a couple of other bits in which are here....

 

public void requestFailed(final String message) 
{
// TODO Auto-generated method stub
UiApplication.getUiApplication().invokeLater(
new Runnable()
{
public void run()
{
FacebookBrowser twitBrowse = new FacebookBrowser(_article, _link);
UiApplication.getUiApplication().pushScreen(twitBrowse);
close();
}
}
);

}

public void requestSucceded(final byte[] result, final String contentType)
{
UiApplication.getUiApplication().invokeLater(
new Runnable()
{
public void run()
{
if(contentType.startsWith("text/"))
{
String strResult = new String (result);
synchronized (UiApplication.getEventLock())
{
if(strResult.indexOf("You have posted to your wall")!= -1)
{
close();
Dialog.alert("Success: Posted to your wall");
//close();
}
else
{
close();
Dialog.alert("Failed "+ strResult);

}
}
}
else
{
synchronized (UiApplication.getEventLock())
{
close();
Dialog.alert("Failed non text");
}
}
}
}
);
}

 It might not be useful for you but its there if you want to use it

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

Thanks for that - but I have indeed tried a  very similar approach - but the httpconnect keeps on doing weird things - and then eventually tries to connect with WAP2 - which results in multiple re-directions - and eventually landinfg up on a wap options page generated by the cell-phone carrier. 

check this: http://supportforums.blackberry.com/t5/Java-Development/Url-getting-mangled/m-p/1129925#M160415

 

Thats why I tried to use the BrowerField class - at least it works !  I am sure if I can just get the listener to work I can retireve the html that is being displayed.

 

 

 

Developer
Posts: 28
Registered: ‎10-05-2009
My Device: Not Specified

Re: Retrieve content from browserfield

[ Edited ]

The coverageTest thing thats in my code will help you out there.....

 

package com.;

import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.WLANInfo;

public class CoverageTest {

public String CoverageTest() {
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();
} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
connectionParameters = ";deviceside=false";
} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
connectionParameters = ";deviceside=true";
}
}

return connectionParameters;
// TODO Auto-generated constructor stub
}

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;
}

}

 Another guy has done a connectionFactory class some where aswell which is meant to be pretty good.

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

That example works - exactly like my previous attempts.  Up to the point where it thows the error:

"Unexpected response code: 302"

 

This again shows that the class does not use the same connection method as the BrowserField - because the letter actually retrieves and displays the page.  Logic says that having come this far - one must just be able to access the underlying html (of the displayed page).

 

 

 

 

 

 

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

This is how I implemented your example:

 

 

void run_new_connect(){

String reply;   
reply=new String("Unknown error");
String id=new String("1234");
String Location_name=new String("Psitest");

String thecommand=new String("http://72.249.183.17:80/?Lines=3&FUNC=BBCONSOLE&Loc="+Location_name+"&ID="+id);
   
    String url;
    String method; //GET or POST
   // FacebookPost screen;
   // byte[] postData;
       
              url = new String(thecommand);
                method = new String("GET");
//              this.screen = networkingMainScreen;
            //this.postData = postData;
            
              try
            {
                      CoverageTest ct = new CoverageTest();
                  if (ct.CoverageTest() != "") 
                  {
                              HttpConnection connection = (HttpConnection)Connector.open(url+ct.CoverageTest());
                             connection.setRequestMethod(method);
                           
/*                               if(method.equals("POST") && postData != null)
                          {
                                      connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
                                    OutputStream requestOutput = connection.openOutputStream();
                                    requestOutput.write(postData);
                                 requestOutput.close();
                         }
                         */
                              
                               int responseCode = connection.getResponseCode();
                               if(responseCode != HttpConnection.HTTP_OK)
                             {
                                      reply=new String("Unexpected response code: "+responseCode);
                                       connection.close();
                                        log_tx=new String(reply);
                                       
                                    return;
                                }
                              
                               String contentType = connection.getHeaderField("Content-type");
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                              
                               InputStream responseData = connection.openInputStream();
                               byte[] buffer = new byte[10000];
                               int bytesRead = responseData.read(buffer);
                             
                               while(bytesRead >0)
                            {
                                      baos.write(buffer, 0, bytesRead);
                                      bytesRead = responseData.read(buffer);
                         }
                              
                               baos.close();
                          connection.close();
                            
                            reply=new String(baos.toByteArray());
                       }
                      else
                   {
                              reply=new String("No Connection");
                 }
                      
               }
              catch(IOException e)
           {
                      reply=new String(e.toString());
            }
      
      
  log_tx=new String(reply);
          
      
      }

// log_tx is then displayed in the result screen

 Could it be bacause I am using GET and not POST ?

 

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

Vishal, can you show us the listener code that you implemented ?

Contributor
Posts: 24
Registered: ‎06-03-2011
My Device: Curve
My Carrier: Vodacom SA

Re: Retrieve content from browserfield

I have now figured out how to retrieve the content of the browserfield.  It can be done thus:

Stringlog_tx=new String();
 
 // create new instance of the BrowserField
 browserField = new BrowserField();

 BrowserFieldListener listener = new BrowserFieldListener() {
     public void documentLoaded(BrowserField browserField, Document document) throws Exception
     {

 HTMLDocument htmlDoc;
 String content=new String();
 

               htmlDoc = (HTMLDocument) document;
                HTMLElement bodyElement = htmlDoc.getBody();
                    content = bodyElement.getTextContent();

                    log_tx=log_tx.concat(content);
       
     }
};

    browserField.setChangeListener(this);
    browserField.addListener( listener );

// at this point you can add_log_tx to a component and display it
// no need to add the browserfield itself

 The snag is that it will always display the PREVIOUS version of the BrowserField.  Therefore the first time you call the screen it will show nothing, but next time it will show the first instance etc.