mobileDeveloper
mobileDeveloper

Reputation: 894

get absolute url on handle ressource request on blackberry

I use this method the get the urls of ressources contain on web page

public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception 
{
    final String url = request.getURL();
    return super.handleResourceRequest(request);
}

But, I made request.getURL(); it returns relative url and not the absolute url.

How can I change it to get the absolute URL?

Upvotes: 1

Views: 165

Answers (1)

Nate
Nate

Reputation: 31045

When I run your code, it does return me absolute URLs, even when my web page contained relative links. That said, it wouldn't surprise me if sometimes, it doesn't. I haven't fully tested this code, but I would think you could try something like this.

Basically, you check to see if the URL is absolute, and if not, you assemble an absolute URL by using the parent BrowserField document URL:

  ProtocolController controller = new ProtocolController(_browserField) {

     public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception {
        String absoluteUrl = null;
        URI uri = URI.create(request.getURL());
        if (uri.isAbsolute()) {
           absoluteUrl = request.getURL();
        } else {
           String docUrl = _browserField.getDocumentUrl();
           String url = request.getURL();
           if (url.startsWith("/")) {
              // the URL is relative to the server root
              URI docUri = URI.create(docUrl);
              absoluteUrl = docUri.getScheme() + "://" + docUri.getHost() + url;
           } else {
              // the URL is relative to the document URL
              absoluteUrl = docUrl + url;
           }
        }
        System.out.println("   requesting: " + absoluteUrl);
        return super.handleResourceRequest(request);
     }
  }

Again, for me, I was getting absolute URLs, so I couldn't easily test the code in the branch where the URL is relative. So, it's possible that I'm dropping a "/" somewhere, or not handling file:/// URLs properly.

But, this is a starting point, to workaround your problem.

Upvotes: 1

Related Questions