0

I have a webview that is working perfectly as follows:

    wv = (WebView) findViewById(R.id.wv);
    //Enable JavaScript
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setFocusable(true);
    wv.setFocusableInTouchMode(true);
    //Set Render Priority To High
    wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    wv.getSettings().setDomStorageEnabled(true);
    wv.getSettings().setDatabaseEnabled(true);
    wv.getSettings().setAppCacheEnabled(true);
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    //Load Url
    wv.loadUrl("https://str8red.com/");

I then have the following code which runs when a button is clicked:

    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
            StringRequest stringRequest = new StringRequest(Request.Method.GET,"https://str8red.com/loggedincheck",

                    new Response.Listener<String>(){
                        @Override
                        public void onResponse(String response) {
                            textView.setText(response);
                            requestQueue.stop();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    textView.setText("Something went wrong");
                    error.printStackTrace();
                    requestQueue.stop();
                }
            });
            requestQueue.add(stringRequest);
        }
    });

The address simply returns a string of "True" or "False" depending if the user is logged into the webview. The good news is that it is returning a string but it is always returning False, even when I have logged in to my site using the webview. I know the address https://str8red.com/loggedincheck works as expected as it works both in a web browser and also in the iOS app.

I am assuming that when the button is clicked it is not using the the same webview and therefore has no idea if the user is logged in or not.

I have looked online for a solution but have come unstuck. Any help would be appreciated.

If you need any additional information please do not hesitate to ask.

Alan Tingey
  • 835
  • 11
  • 39

1 Answers1

1

Indeed it will be always return false, because there is no relation between your Volly code and your webView code, so the right way based on this answer, you have to use evaluateJavascript instead of volley, by passing your javaScript function you can know user case.

wv.evaluateJavascript("(function() { return 'this'; })();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); // Prints: "this"

        //check for user case based on javaScriptFunction
    }
});
  • Ok, so just have to use a different approach on android as opposed to iOS. Appreciate the swift response. – Alan Tingey Jun 07 '17 at 21:39
  • I don't know if there an easier way, but the code won't able to have relation between webView and `volley` code. –  Jun 07 '17 at 21:42
  • 1
    That is ok, I will remove the volley part. So when they login I will just have some variables within javascript on that page and pass them back to the native app using wv.evaluateJavascript as described. May even be a cleaner solution for the iOS version too. I hope what I have said makes sense and means I have understood your response. – Alan Tingey Jun 07 '17 at 21:45
  • that is exactly what i mean best of luck. –  Jun 07 '17 at 21:46
  • Great news, and I am happy I understood. Go me lol. Take care Ibrahim. – Alan Tingey Jun 07 '17 at 21:47