Ask away

Android Calling JavaScript functions in Button

Kevin 11/12/17 08:46


Question CategoryMiscellaneous

Number of completed projects0

Question StatusUnsolved

  • android
  • js
  • button


I've got a Button that button need to access some Javascript function. Simply my app get the user info(ID,pass) then go to web page(this operation doing backgrun with asynctask class) write these two info as ID and pass then user click the Log In button in my app button has to use some js function <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['form1']; if (!theForm) { theForm = document.form1; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } //]]> </script> this is the func. i need to use My post and get request for connection the site are POST// URL url = new URL(params[0]); // site= connection=(HttpURLConnection) url.openConnection(); connection.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(data); writer.flush(); these codes for the put the ID and pass GET // reader= new BufferedReader((new InputStreamReader(connection.getInputStream()))); StringBuilder builder = new StringBuilder(); String line; while((line= reader.readLine())!=null){ builder.append(line + "\n"); } text=builder.toString(); there is any help or suggestion for me i am very confused about that situation and i feel really bad myself thanks for helps anyway.


Dave 11/24/17 11:53

Is there a reason you don't use a hidden WebView and simply inject your code? // Create a WebView and load a page that includes your JS file webView.evaluateJavascript(";", null);

Todd 11/26/17 20:17

In android we use ready-made library to make http requests. Google has a good library called Volley. it is easy to customize,respond to errors and it automatically making request out of the main thread

John 11/29/17 13:22

You can use AndroidJSCore. Here is a quick example how you might do it: HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://your_website_here/file.js"); HttpResponse response = client.execute(request); String js = EntityUtils.toString(response.getEntity()); JSContext context = new JSContext(); context.evaluateScript(js); context.evaluateScript(";"); However, this most likely won't work outside of a WebView, because I presume you are not only relying on JavaScript, but AJAX, which is not part of pure JavaScript. It requires a browser implementation.

Dean 11/30/17 09:28

Yes you can make HTTP POST and HTTP GET requests without using WebView. But if you want to use a webView remember Javascript in a webview is disabled by default (for security purposes). So before calling any javascript functions make sure you enable javascript in your webview like this webView.getSettings().setJavaScriptEnabled( true ); And after that javascript will be enabled in your webView. But in case you do not want to use a webview and javascript to make http requests. There is a lot of alternative methods you can define a Button in your activity's layout in xml. And respond with a http request on button Clicked listener! Also remember making http Request using Android/Java default classes is a huge task and error prone and requires you to care about using async tasks to avoid blocking the UI thread.