Is it possible to make javascript wait for a given async of android?

Asked

Viewed 77 times

0

Imagine this scenario: I have an application developed in Angularjs that runs in some environments like proprietary hardware and computers. We are currently adding our software to the android platform and we are having some problems.

Our application in Angular is developed all by making HTTP calls, ie the environment we are running the app (yes, it runs on localhost) just need to have our webservice running.

On android, we managed to create a webservice only with several problems like:

• In some Vices, after the memory exceeds 50mb or 75mb, the application is closed.

• If the app is running on a service (which is our case), it can only be restarted after crashing in a long time, otherwise the Android OS will not restart the service.

• An application is not meant to be an http server (in theory, rs)

Due to these problems and some others, we tried to adopt another solution which is the use of Javascriptinterface. With it is possible to call android methods via javascript (I believe that IONIC and company use something like this).

So here’s my gambiarra attempt: In order not to have millions of IFS in my services saying who I want to call, I tried to create an angled Interceptor, only I had some problems..

If in my android method I have an assync call, it is possible to make javascript wait to return?

The idea of the Interceptor is all HTTP call to fall into the method responseError, since the webservice does not exist, and when enter the responseError, make the call to android, something like this:

app.factory('httpInterceptor', function($q){
    return {
        'responseError': function(config) {
            var deferred = $q.defer();
            deferred.resolve(window.PFL.getSmfFile());
            return deferred.promise;
        }
    }
})

And on my android, I have this:

    package myapplication;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.WebView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new JSInterfaceManager(this), "PFL");
        webView.loadUrl("file:///android_asset/interceptors.html");
    }

    public class JSInterfaceManager {

        String result = "Empty";
        Activity activity;

        public JSInterfaceManager(Activity context) {
            this.activity = context;
        }

        @JavascriptInterface
        public String getSmfFile() throws InterruptedException {

            getData(new DataCallBack() {
                @Override
                public void onSucess(String data) {
                    result = "DEU CERTO";
                    Toast.makeText(activity, "oi", Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onError(VolleyError error) {
                    result = "DEU ERRO";
                    Toast.makeText(activity, "oi error", Toast.LENGTH_SHORT).show();
                }
            });


            return result;
        }

        private void getData(final DataCallBack callBack) {
            String URL = "someLink";
            StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    callBack.onSucess(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    callBack.onError(error);
                }
            });
            HTTP.getInstance().addToRequestQueue(stringRequest);
        }

    }


}

public class HTTP extends Application {

    private static final String TAG = HTTP.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static HTTP mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized HTTP getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

}

This approach using Volley was an attempt to return the call in synchronous mode (I saw this on a blog and tried to implement).

My question is: how do I make an http call from android and return to the angular http call. Is it possible? There are other means?

Sorry for the giant question

  • I don’t know if I understood your question very well, but if you really just make the request on Android and when you finish returning to Javascript, why don’t you create a plugin for Cordova that will only be the bridge between the JS and the native? With this, you can run the code on Android, do what you have to do and when finished, call the callback from there Angular.

  • @Paul, only to try to understand his need. You will make a request from Android to the Node server, the server will receive this call and then make another call to an Android device is this ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.