0

I've an Android Activity and 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]); //http://login.cu.edu.tr/Login.aspx?     site=https://derskayit.cu.edu.tr&ReturnUrl=%2f
            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. Have a nice day

2
  • 1
    What are you asking? What is your question? Commented Sep 23, 2017 at 22:04
  • Is there any way to use this function on my program without usiing webview? @Xenolion Commented Sep 24, 2017 at 5:53

1 Answer 1

0

For using Javascript without a webView to make requests!

The question has already an answer here in this question

The javax.script package is not part of the Android SDK. You can execute JavaScript in a WebView, as described here. You perhaps can use Rhino, as described here. You might also take a look at the Scripting Layer for Android project.

Also a similar question was asked here

You can execute JavaScript without a WebView. 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("question.vote(0);");

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.

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("question.vote(0);", null); 

Otherwise:

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.

Alternatively

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.See more explanation here!. If there is still some problems comment below!

Sign up to request clarification or add additional context in comments.

3 Comments

First of all thanks for helping. I already use asynctask to avoid the errors etc. but i want to use that javascriptfunction without the webview, i want my button(called log in) use the javascript function to log in the site than i can use the info inside the site in my UI there is a way to do that @Xenolion
No its not about Volley its about using Javascript without webview I have edited my answer to include links to those questions! And ofcourse Volley is good!
Thanks a lot for helping its really helpful for me this information i will try to implement these in my code. I will notify you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.