Use kivy urlRequest

2

How exactly do you use kivy's UrlRequest? In case I wanted to download a page that returns a json file. Using the python requests library I can do this easily, however at the time of running the application on the android mobile phone, it gets minimized by itself, so I need an alternative. This is the code I have using requests:

url = requests.get('pagina_web.json')
page = url.json()
page2 = page['chave1']['chave2']['chave3']['chave4']

I need something similar using UrlRequest or another library, but it works at the time of running the application.

    
asked by anonymous 31.12.2017 / 01:33

1 answer

1

I think this app explains how Kivy's UrlRequest works:

from kivy.network.urlrequest import UrlRequest
from kivy.app import App
from kivy.uix.button import Button



class reqApp(App):
    def build(self):
        bt =  Button(text='Pegar Json do Bitcoin')
        bt.bind(on_press=self.make_request)
        return bt

    def make_request(self, instance):
        UrlRequest('https://www.mercadobitcoin.net/api/BTC/ticker/', self.print_json)

    def print_json(self, req, result):
        print result['ticker']

if __name__ == '__main__':
    reqApp().run()

When you click the button it calls the make_request that makes an instance of UrlRequest . When the object is instantiated, it automatically makes an asynchronous request and when it finishes (if the request succeeds) it calls the function print_json .

Remember it's asynchronous so the GUI main loop will not "hang" until the request is finished, there is no need to use the python threads.

To get all the details of the parameters that you can pass in UrlRequest see link

PS: I use the library requests in android in an app that made music and works normal if you want to see: link

If it is very simple you can use the standard python library urllib2

    
31.12.2017 / 21:43