Web Requests and Android

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Web Requests and Android

#1 Post by w022a »

Hi folks,

I'm after a little advice on web requests with Android. I am using the modified version of the python requests module for Ren'Py (https://github.com/renpytom/renpy-requests) to download some personalisation data and send to server for processing with a simple REST API. Later in the game, I will use it to publish data.

All is working well in Windows, but when I build the game for Android, it throws a runtime error, below

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/000statements.rpy", line 414, in execute_pause
  File "renpy/common/00action_other.rpy", line 517, in __call__
  File "game/script.rpy", line 47, in webRequest
ImportError: cannot import name HTTPConnectionPool

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "script.rpyc", line 102, in script
  File "/data/user/0/com.egr.lost/files/renpy/ast.py", line 1828, in execute
    self.call("execute")
  File "/data/user/0/com.egr.lost/files/renpy/ast.py", line 1816, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "/data/user/0/com.egr.lost/files/renpy/statements.py", line 177, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/000statements.rpy", line 414, in execute_pause
  File "/data/user/0/com.egr.lost/files/renpy/exports.py", line 1452, in with_statement
    return renpy.game.interface.do_with(trans, paired, clear=clear)
  File "/data/user/0/com.egr.lost/files/renpy/display/core.py", line 2207, in do_with
    clear=clear)
  File "/data/user/0/com.egr.lost/files/renpy/display/core.py", line 2649, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "/data/user/0/com.egr.lost/files/renpy/display/core.py", line 3451, in interact_core
    rv = root_widget.event(ev, x, y, 0)
  File "/data/user/0/com.egr.lost/files/renpy/display/layout.py", line 992, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/data/user/0/com.egr.lost/files/renpy/display/transition.py", line 45, in event
    return self.new_widget.event(ev, x, y, st)  # E1101
  File "/data/user/0/com.egr.lost/files/renpy/display/layout.py", line 992, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/data/user/0/com.egr.lost/files/renpy/display/layout.py", line 992, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/data/user/0/com.egr.lost/files/renpy/display/screen.py", line 697, in event
    rv = self.child.event(ev, x, y, st)
  File "/data/user/0/com.egr.lost/files/renpy/display/layout.py", line 992, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "/data/user/0/com.egr.lost/files/renpy/display/behavior.py", line 1993, in event
    return run(self.function, *self.args, **self.kwargs)
  File "/data/user/0/com.egr.lost/files/renpy/display/behavior.py", line 315, in run
    return action(*args, **kwargs)
  File "renpy/common/00action_other.rpy", line 517, in __call__
  File "game/script.rpy", line 47, in webRequest
  File "/data/user/0/com.egr.lost/files/renpy/loader.py", line 723, in load_module
    exec code in mod.__dict__
  File "requests/__init__.py", line 65, in <module>
  File "/data/user/0/com.egr.lost/files/renpy/loader.py", line 723, in load_module
    exec code in mod.__dict__
  File "requests/api.py", line 13, in <module>
  File "/data/user/0/com.egr.lost/files/renpy/loader.py", line 723, in load_module
    exec code in mod.__dict__
  File "requests/sessions.py", line 26, in <module>
  File "/data/user/0/com.egr.lost/files/renpy/loader.py", line 723, in load_module
    exec code in mod.__dict__
  File "requests/adapters.py", line 15, in <module>
  File "/data/user/0/com.egr.lost/files/renpy/loader.py", line 723, in load_module
    exec code in mod.__dict__
  File "requests/packages/urllib3/poolmanager.py", line 12, in <module>
ImportError: cannot import name HTTPConnectionPool



Has anyone had any success with Android and Requests?


The other question I have in this area is that I want to use the renpy.invoke_thread for the web interactions so it doesn't block the main thread. But what is the best approach for allowing renpy to pass data between the main and daemon thread?

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Web Requests and Android

#2 Post by xavimat »

Two examples in this thread: viewtopic.php?f=8&t=47568
One from pytom that uses requests and certifi and another one mine using only python2 modules.

With the older python2 modules I've succesfully made a program for Android that uploads/downloads info from a server (not https, though, only http).
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Re: Web Requests and Android

#3 Post by w022a »

Thanks for the help I tried using urllib2 as you're done in your game. This is working well in Android for me too, so if I can't get "Requests" working, I could change my implementation to use this..

I did try doing an Android build of the sample "News Feed" game by pytom that looks to use the modified version of "Requests". This didn't work either. So i guess there's something a bit broken with ren'py requests and android (or something I'm missing in the build).

Are you running your web handling stuff in a seperate thread? if so, how did you pass the data between the threads?

thanks.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Web Requests and Android

#4 Post by xavimat »

w022a wrote: Sun Jun 24, 2018 5:22 pmAre you running your web handling stuff in a seperate thread? if so, how did you pass the data between the threads?
Yes, I'm using $ renpy.invoke_in_thread(connect) but I'm not doing anything to pass the data to the main thread, renpy does it.
As you can see in the news example, when first the game starts, the news button in the main menu shows nothing, and half second later it gets the info and changes.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Re: Web Requests and Android

#5 Post by w022a »

I did manage to get Requests working on Android. I updated the requests package and dependencies to those packages in the pytom news demo.

I found the problem with the pytom news demo not working on android was this;

Code: Select all

CA_FILE = os.path.join(config.gamedir, certifi.where())
Android couldn't resolve the path. This is an interesting issue as requests get method expects the cacert to be specified as a file path, so we can't read in the data from ren.py.file() and pass it that way. I'm sure there's a simple solution but, i'm not using an HTTPS server as the data i'm requesting isn't sensitive, so I don't need to pass a certificate over.

For passing data between the thread invoked by renpy.invoke_in_thread() , i'm still not sure i'm doing this efficiently. I couldn't get the return value from the thread to update a variable in the main thread. I would really like to invoke some kind of callback to perform a jump as soon as the daemon thread finishes based on the return value, but haven't found a way to do this.

For now, I am updating a persistent.variable in the daemon thread which can be accessed by the main thread, then a screen timer to periodically check the state and perform a jump if the value changes. But this seems a bit inefficient.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]