Async way to send/load data on Android?

On Android the Loader/URLLoader etc cause insane app freezes and stuttery animation, we’ve seen it in effect with google analytics but also with very simple json and images. Using haxe.Http to fetch files also blocks.

Is there an asynchronous way to do this within haxe/openfl?

Have you tried using HTTPService instead of URLLoader?
It is just a suggestion, try it and see if it doesn’t freeze anymore.

What’s that? I can’t find much on Google about it. We’re using Java where we can so we can put downloads etc on background threads but it’s not very cross-platform.

sorry, my fault… it is supported only in flash, no openfl. (sometimes I look on google for AS3 answers, and often they are supported in openfl as well… not this case :smile: )

I’ve got the same problem anyway, I am looking around for something really async.
(when I call the .load(…) sometimes it freeze for half a second on almost every platform (ios, mac, windows, linux), especially if there is a slow connection)

I’m trying a different way, I keep you posted :wink:

Almost done :wink:

I’m getting the solution via Sockets and multi threading.
I’ll post it on github as soon as it is finished :wink:

What about using threading?

import cpp.vm.Thread;
function loadAssets()
{
     //loader stuff
}
function main()
{
     var worker:Thread = Thread.create(loadAssets);
     worker.sendMessage(Thread.current ());
}

update
I’ve just tested this on windows and it doesn’t seem to work on sending urlrequestes :confused: The stuttering is still there. Works fine with drawing lots of bitmapdatas though…

all right.

I’ve written this class
https://github.com/yupswing/plik/blob/master/com/akifox/plik/net/AsyncHttpRequest.hx

It supports HTTP requests (HTTP/1.1), only GET method.
and right now it is written to get Strings (not the best to load raw data, like bitmaps) but good for text (html, json, csv… etc etc)

I’m planning to developing it more, it is just a prototype.


Basically it use the standard URLRequest/URLLoader on flash and html5
but on CPP, NEKO and JAVA it supports multi-threading.

In my case there were a freeze, sometimes, especially on slow connection.
Using this class there is no more freeze, and it is compatible with all targets in a way or another.


How to use it?

Basically you can copy the class in your project and use it as it follows:

package ;

class Main {
   function new() {
       //AsyncHttpRequest.logEnabled = true;
       AsyncHttpRequest.send("http://fredfgtaefc.com:8080/mypage?field=test&field2=test",function(response:AsyncHttpResponse){
           trace(response.request,response.status,response.time);
       });

       AsyncHttpRequest.send("http://en.wikipedia.org/wiki/Haxe",wikipediaPage);
       AsyncHttpRequest.send("http://en.wikipedia.org/wiki/OpenFL",wikipediaPage);
   }

   function wikipediaPage(response:AsyncHttpResponse) {
      trace(response.request,response.status,response.time);
      if(response.content==null) {
        trace('error');
      } else {
        trace('done'); 
      }
   }
}

in the AsyncHttpResponse you get 4 properties
request: the original url requested
status: 0 if error otherwise a normal HTTP Response Status
time: seconds elapsed between the request and the response
content: a string with the response content

@ben if you want to test it and see if it can be useful to you please do it.
if you like it and you want to improve it please fork it and make a pull request.

see ya

2 Likes

Awesome! You should upload it to haxelib :slight_smile:

Good idea.
I’ve made a repository for the library


News:
it supports HTTP/1.1 almost completely (POST, GET, PUT, DELETE) with content payload on the request.
also I’ve made it a little bit more organised with an Request Object and a Response Object.

You can see a good example looking at the README on the repository


Haxelib
I’m going to submit on haxelib very soon (as soon as I recover my password :facepalm:)

it is live on haxelib

haxelib install akifox-asynchttp
2 Likes

sorry I didn’t catch well,
is it possible to send and get request?
I mean I have a an app that sends the player’s scores to a web service in order to update the leader board of the app???

Is URLLoader still synchronous on current versions? Are you using -Dlegacy?

My bad I am still stuck on openfl 2.2.8 because a project I am making is having hard time to run on anything later up to openfl 3.1.1(Still have to figure out why)
I was just looking at sending score to a webservice and retrieving a leaderboard and I bumped into this post.

Just to make it clear.

URLLoader is built-in tool in OpenFL and it works good as far as I know (it did have some problems of freezing for few moments from time to time and that lead me to develop akifox-asynchttp), but my implementation offers an api common to all targets, provides a lot more options, it is usually faster in fetching a request and it does not need OpenFL (although it is obviously compatible with it).

To answer @ray_diama question, YES, it does send a request and get a response.
Look at the code or just the README to understand better what it does.

1 Like

@ray_diama Does using -Dlegacy when building with OpenFL 3 work for you?

@singmajesty yes it works fine thanks

@yupswing thank you one more time