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

Native Development

Reply
Developer
1234321
Posts: 24
Registered: ‎10-05-2013
My Device: Z10
Accepted Solution

Encode part of URL (search)

I want to be able to do a Google search in a webview, by setting the URL to "https://www.google.com/search?&q=" + "search".

The problem is that if the search query contains a + sign, it get interpreted as a space (this actually happens in some popular apps including Evolution browser).

 

I read that encodeURIComponent should fix this problem so I changed the code to "https://www.google.com/search?&q=" + encodeURIComponent("+"), but the resulting search is actually for %2B.  Strangely, when I type the resulting URL into my computer I get the corrrect result (a search for +).

 

import bb.cascades 1.2


Page {
    property string search: "https://www.google.com/search?&q=" + encodeURIComponent("+") 
    Container {
        TextField {
            text: web.url 
        }
    WebView {
        id: web
        url: search
    }
    }
}

 

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Encode part of URL (search)

Hi,

 

This is an effect of two things:

  • QUrl's constructor treats all input as NOT escaped.
  • QML/Javascript passes URLs around as string.

So - you're seeing the code getting double escaped.

 

I worked around this like this:

import bb.cascades 1.0

Page {
    property string search: "https://www.google.com/search?&q=" + encodeURIComponent("+")
    Container {
        WebView {
            id: web
            onCreationCompleted: {
                app.setURL(web, search);
            }
        }
    }
}

 Functions for the manipulation:

    Q_INVOKABLE QUrl urlMagic(QString source) {
    	QByteArray data(source.toAscii());
    	QUrl toReturn;
    	toReturn.setEncodedUrl(data);
    	return toReturn;
    }

    Q_SLOT void setURL(bb::cascades::WebView * webView, QString url) {
    	if (webView!=NULL) {
    		QUrl toSet(urlMagic(url));
    		webView->setUrl(toSet);
    	}
    }

 Found I also needed to register WebView (i'm working with older SDK ATM)

    qmlRegisterType<bb::cascades::WebView>("bb.cascades",1,0,"WebView");

 Hope this helps...

 

Thanks!

--
Rob is no longer associated with BlackBerry.
Developer
1234321
Posts: 24
Registered: ‎10-05-2013
My Device: Z10

Re: Encode part of URL (search)

Hello,

Thank you for your reply.

I am new to coding and don't really understand where to put the functions.

Could you show me the full files?

 

Thank you

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Encode part of URL (search)

Hi,

 

Have you made any progress?

 

The first block goes in your QML, the second goes in your c++ header file (in the public: section). The third goes in main.cpp before cascades calls.

 

You'll also need :

* Correct includes to find these classes

* Insertion of the c++ class into the QML context.

 

Thanks.

--
Rob is no longer associated with BlackBerry.
Developer
1234321
Posts: 24
Registered: ‎10-05-2013
My Device: Z10

Re: Encode part of URL (search)


robbieDubya wrote:

 

You'll also need :

* Correct includes to find these classes

* Insertion of the c++ class into the QML context.



I don't know how to do this.

 


robbieDubya wrote:

 

the second goes in your c++ header file (in the public: section).


I keep getting the error "return type 'struct QUrl' is incomplete" and "'bb::cascades::WebView' has not been declared".

 

 

Thank you

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Encode part of URL (search)

Here's the minimal project I used to test my code.

--
Rob is no longer associated with BlackBerry.
Developer
1234321
Posts: 24
Registered: ‎10-05-2013
My Device: Z10

Re: Encode part of URL (search)

Thank you! I have got it working now.