icicleking
icicleking

Reputation: 1077

In kivy python how to get data from a url request

I am requesting JSON from a url in kivy. In the main App:

data = UrlRequest('http://myhost.ninja/request', gotArticles, onFailure)

and the callback:

def gotArticles(req, results):
  for key in results:
    return key

when I then print(data) I get <UrlRequest(Thread-1, started daemon 123145307557888)> printed. I can print the data from inside the callback, but how do I return the data to the rest of the application?

Upvotes: 3

Views: 4485

Answers (1)

Peter Badida
Peter Badida

Reputation: 12159

I think it's pretty obvious that you are not attempting to print a function's return(like print(int('1'))), but rather object i.e. the whole UrlRequest class that you assigned to data variable. Same as print(TextInput())

Try to print a variable from that object:

print(data.result)

which is basically UrlRequest.result.

Example:(http is intended only to show that result is present, the real page has https)

req = UrlRequest('http://kivy.org')
while not req.is_finished:
    sleep(1)                         # seems to be unnecessary in this case
    Clock.tick()

print('result =', req.result)
print('error =', req.error)

wait() does visually the same thing

req = UrlRequest('http://kivy.org')
req.wait()
print('result =', req.result)
print('error =', req.error)

Upvotes: 4

Related Questions