Is it possible to have the javascript wait for an async given android?

0

Imagine this scenario: I have an application developed in angularjs that runs in some environments such as proprietary hardware and computers. We are currently adding our software to the android platform and are having some issues.

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

In android, we were able to create a webservice only with several problems like:

• On some devices, after the memory exceeds 50mb or 75mb, the application is closed.

• If the application is running on a service (which is our case), it can only be restarted after cracking for a long time, otherwise the Android OS does not restart the service.

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

Due to these issues and a few others, we tried to adopt another solution that is using JavascriptInterface . With it it is possible to call android methods via javascript (I believe that IONIC and company use something like this).

So here's my attempt at gambiarra: In order to not have millions of IFS in my services saying who I want to call, I tried to create an interceptor in the angle, but I had some problems ..

If in my android method I have an assync call, can you make the javascript wait to return?

The idea of the interceptor is all called HTTP fall in the responseError method, since the webservice does not exist, and when entering the responseError, make the call to the 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 synch mode (I saw this on a blog and tried to implement).

My question is: How do I get an http call from android and return on the angular http call. It's possible? Are there other ways?

Sorry for the giant question

    
asked by anonymous 31.05.2016 / 16:52

0 answers