I am currently making a hello world type Phonegap application for android, In which I call a javascript function from the native android code to set the text of one of the paragraphs in my app.
I have a javascript function that I define in my app's html, and I want to call it from my activity once the app loads up. The function just sets the text of a paragraph in my app. I cannot get this to work.
Here is my java activity code:
public class MyPhoneGapActivity extends DroidGap {
public DroidGap c;
public LoadInfo info;
#SuppressLint("SetJavaScriptEnabled")
#JavascriptInterface
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
c = this;
appView.getSettings().setJavaScriptEnabled(true);
appView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
appView.loadUrl("file:///android_asset/www/index.html");
appView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
MyPhoneGapActivity.this.runOnUiThread(new Runnable() {
public void run() {
c.sendJavascript("javascript:getMassTimes()");
}
});
}
});
}
}
and here is my javascript and html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Ionic Seed App</title>
<link href="lib/css/ionic.css" rel="stylesheet">
<link href="css/app.css" rel="stylesheet">
<link href="lib/css/mycss.css" rel="stylesheet">
<script src="lib/js/ionic.js"></script>
<script src="lib/js/angular/angular.js"></script>
<script src="lib/js/angular/angular-animate.js"></script>
<script src="lib/js/angular/angular-sanitize.js"></script>
<script src="lib/js/angular-ui/angular-ui-router.js"></script>
<script src="lib/js/ionic-angular.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
</head>
<body ng-app="myApp">
<script type="text/javascript">
function getMassTimes() {
document.getElementById("massTimes").textContent = "This is some text";
}
</script>
<div>
<header-bar title="Saint David the King"
class="bar bar-header bar-energized">
<h1 class="title">Saint David the King</h1>
</header-bar>
<br>
<content scroll="true" has-header="true" width="100%" height="100%"
id="content"> <img alt="Saint David the King"
src="file:///android_asset/www/img/saintdavidthekingbackground2.png"
width="100%" /> <br>
<p id="massTimes">
</p>
</content>
</div>
</body>
</html>
Is there something I am doing wrong?
I have seen these sites:
Resolved - Call javascript function from Java
How to call javascript function from java code
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/1o9PgfmyRd8
http://www.jumpbyte.com/2012/phonegap-native-to-javascript/
Javascript calls from Android using PhoneGap
how to call javascript function from android using phonegap plugin
And I have tried what they have on them, but I just can't get this to work.
Any help appreciated!
Replace c.sendJavascript("javascript:getMassTimes()"); with
myBrowser.loadUrl("javascript:getMassTimes()");. This worked for my problem.
If you want to pass a String from the activity to javascript use
String msgToSend = "Hello World!"; myBrowser.loadUrl("javascript:callFromActivity(\""+msgToSend+"\")");
Related
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet{
/**
*
*/
private static final long serialVersionUID = 1L;
String dString = "Value is null";
String gPath="";
public void get_path(){
gPath = this.getParameter("path");
if(gPath == null)
gPath = dString;
//setBackground(Color.cyan);
}
public void paint(Graphics g){
g.drawString(gPath, 20, 30);
}
}
HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title> Upload File </title>
<script type="text/javascript">
var sys_path;
function print_path(){
sys_path = document.getElementById('name').value;
document.app.get_path();
document.write(sys_path);
}
</script>
<applet code="MyApplet.class" name="app" width="300" height="200">
<param name="path" value="&{sys_path};">
</applet>
</head>
<body>
<form>
Path:<input type="text" id="name" \>
<input type="button" value="click" onClick="print_path();" />
</form>
</body>
</html>
Explanation:
In these code I'm trying to take input from text box and display in the applet. I'm trying this couple of days and unable to find a way. Please help me regarding this. Thanks in advance!
I'm using scribe to connect with QTIWorks
I found this question How to create a oAuth request using java?
I write this QTIWorks class
package testscribe;
import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;
import org.scribe.model.Verb;
public class QTIWorks extends DefaultApi10a {
#Override
public Verb getRequestTokenVerb()
{
return Verb.GET;
}
#Override
public String getRequestTokenEndpoint() {
return "https://webapps.ph.ed.ac.uk/qtiworks-dev2/lti/domainlaunch";
}
#Override
public String getAccessTokenEndpoint() {
return "none";
}
/*#Override
public String getAuthorizationUrl(Token requestToken) {
return "none";*/
#Override
public String getAuthorizationUrl(Token token) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
and this is Main class
package testscribe;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
public class Main {
public static void main(String[] args){
OAuthService service = new ServiceBuilder().provider(QTIWorks.class).apiKey("gmail.com/zeina.helwani").apiSecret("v6wPuluQXPwX3vva71ZpR7i1fsbGPaT6")
.scope("API.Public").build();
Token requestToken = service.getRequestToken();
OAuthRequest request = new OAuthRequest(Verb.POST,"https://webapps.ph.ed.ac.uk/qtiworks-dev2/lti/domainlaunch");
service.signRequest(requestToken, request);
Response response = request.send();
System.out.println(response.getBody());
}
}
but it is give me this error
Exception in thread "main" org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QTIWorks - Method Not Allowed (HTTP Error 405)</title>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:400,400italic,700,700italic|Ubuntu:500">
<link rel="stylesheet" href="/qtiworks-dev2/lib/960/reset.css">
<link rel="stylesheet" href="/qtiworks-dev2/lib/960/text.css">
<link rel="stylesheet" href="/qtiworks-dev2/lib/fluid960gs/grid.css">
<link rel="stylesheet" href="/qtiworks-dev2/includes/qtiworks.css?v=1.0-SNAPSHOT">
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script src="/qtiworks-dev2/includes/qtiworks.js?v=1.0-SNAPSHOT"></script>
<!--[if lt IE 9]><script src="/qtiworks-dev2/lib/html5shiv.min.js"></script><![endif]-->
</head>
<body class="page">
<div class="container_12">
<header class="pageHeader">
<h1>QTIWorks</h1>
</header>
<h2>Method Not Allowed (HTTP Error 405)</h2>
<p>You used an HTTP method which is not allowed here.</p><h3>Further Details</h3>
<strong>Message:</strong> Request method 'GET' not supported<br>
<strong>Status Code:</strong> 405<br>
<strong>Request URI:</strong> /qtiworks-dev2/lti/domainlaunch<br>
<div class="clear"></div>
<footer>
<div class="logos">
<img src="/qtiworks-dev2/includes/images/jisc75.png" width="75" height="50" alt="JISC Logo" />
<img src="/qtiworks-dev2/includes/images/uoe.png" width="60" height="60" alt="University of Edinburgh Logo" />
</div>
<div class="copyright">
<p>
QTIWorks 1.0-SNAPSHOT ‒ Release notes
</p>
<p>
Copyright © Thu Apr 09 13:16:35 BST 2015
The School of Physics and Astronomy,
The University of Edinburgh.
</p>
<p>
Contact: David McKain
</p>
<p>
The University of Edinburgh is a charitable body, registered in Scotland,
with registration number SC005336.
</p>
</div>
</footer>
</div>
</body>
</html>
at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:64)
at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:40)
at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:45)
at testscribe.Main.main(Main.java:24)
Java Result: 1
I think this url "https://webapps.ph.ed.ac.uk/qtiworks-dev2/lti/domainlaunch" is invalid, but I don't know what I should use.
I am adding a parameter in the url of the page of I was trying to go but in the generated url it is not being shown, why is that?
Here's my jsp.
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<sj:head ajaxhistory = "true" ajaxcache="true" />
<script>
</script>
</head>
<body>
<h5>Struts Jquery Ajax Integration</h5>
<div id="resultContent"></div>
<noscript>Please Turn On Javascript to make the full use of this site</noscript>
<h4>Choose A task</h4>
<ul>
<s:url value="views/ajaxvalidation.jsp" var="ajaxvalidation" >
<s:param name="menuId" value="1"/>
</s:url>
<li><sj:a targets="resultContent" href="%{ajaxvalidation}">Ajax Validation</sj:a></li>
</ul>
<div>
<h6>Play A Music while You Navigate</h6>
<audio src="x.mp3" controls>Your browser does not support the
audio element.
</audio>
</div>
</body>
</html>
The URL That is being shown is this
http://localhost:8090/HelloStruts2/#resultContent=_sj_action_anchor_860825673resultContent
Where is the menuId parameter that I have added in the url?
I do not know if this will make a difference but I am using this plugin for jquery.
struts2-jquery
Generated HTML
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="/HelloStruts2/struts/js/base/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/HelloStruts2/struts/js/base/jquery.ui.core.min.js?s2j=3.5.0"></script>
<script type="text/javascript" src="/HelloStruts2/struts/js/plugins/jquery.subscribe.min.js"></script>
<script type="text/javascript" src="/HelloStruts2/struts/js/plugins/jquery.ba-bbq.min.js"></script>
<script type="text/javascript" src="/HelloStruts2/struts/js/struts2/jquery.struts2.min.js?s2j=3.5.0"></script>
<script type="text/javascript">
$(function() {
jQuery.struts2_jquery.version="3.5.0";
jQuery.scriptPath = "/HelloStruts2/struts/";
jQuery.struts2_jquery.ajaxhistory = true;
jQuery.ajaxSettings.traditional = true;
jQuery.ajaxSetup ({
cache: false
});
jQuery.struts2_jquery.require("js/struts2/jquery.ui.struts2.min.js");
jQuery(window).trigger('hashchange');
});
</script>
<link id="jquery_theme_link" rel="stylesheet" href="/HelloStruts2/struts/themes/smoothness/jquery-ui.css?s2j=3.5.0" type="text/css"/>
<script>
</script>
</head>
<body>
<h5>Struts Jquery Ajax Integration By Kyel</h5>
<div id="resultContent"></div>
<noscript>Please Turn On Javascript to make the full use of this site</noscript>
<h4>Choose A task</h4>
<ul>
<li><a id="anchor_2068827505" href="javascript:void(0)">Ajax Validation</a>
<script type='text/javascript'>
jQuery(document).ready(function () {
var options_anchor_2068827505 = {};
options_anchor_2068827505.jqueryaction = "anchor";
options_anchor_2068827505.id = "anchor_2068827505";
options_anchor_2068827505.targets = "resultContent";
options_anchor_2068827505.href = "views/ajaxvalidation.jsp";
options_anchor_2068827505.hrefparameter = "menuId=1";
jQuery.struts2_jquery.bind(jQuery('#anchor_2068827505'),options_anchor_2068827505);
});
</script></li>
<li><a id="anchor_1381525763" href="javascript:void(0)">Thank you JSP</a>
<script type='text/javascript'>
jQuery(document).ready(function () {
var options_anchor_1381525763 = {};
options_anchor_1381525763.jqueryaction = "anchor";
options_anchor_1381525763.id = "anchor_1381525763";
options_anchor_1381525763.targets = "resultContent";
options_anchor_1381525763.href = "views/thankyou.jsp";
options_anchor_1381525763.hrefparameter = "menuId=2";
jQuery.struts2_jquery.bind(jQuery('#anchor_1381525763'),options_anchor_1381525763);
});
</script></li>
</ul>
<div>
<h6>Play A Music while You Navigate</h6>
<audio src="x.mp3" controls>Your browser does not support the
audio element.
</audio>
</div>
</body>
Use action tag and use nested param tag see below code .
<s:url id="login" action="admin/showProfile" var="profileUrl">
<s:param name="user">Rais</s:param>
</s:url>
<a href='<s:property value="#profileUrl"/>'>
<s:property value="#profileUrl"/></a>
I am trying to learn AJAX with JSP and I have written the following code. This does not seem to be working. Kindly help:
This is my configuration_page.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var config=document.getElementById('configselect').value;
var url="get_configuration.jsp";
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2 align="center">Saved Configurations</h2>
Choose a configuration to run:
<select name="configselect" width="10">
<option selected value="select">select</option>
<option value="Config1">config1</option>
<option value="Config2">config2</option>
<option value="Config3">config3</option>
</select>
<button type="button" onclick='loadXMLDoc()'> Submit </button>
<div id="myDiv">
<h4>Get data here</h4>
</div>
</body>
</html>
This is my get_configuration.jsp which I am trying to access from the AJAX code above:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h4>Mee..</h4>
</body>
</html>
I have used jQuery AJAX to make AJAX requests.
Check the following code:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#call').click(function ()
{
$.ajax({
type: "post",
url: "testme", //this is my servlet
data: "input=" +$('#ip').val()+"&output="+$('#op').val(),
success: function(msg){
$('#output').append(msg);
}
});
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
input:<input id="ip" type="text" name="" value="" /><br></br>
output:<input id="op" type="text" name="" value="" /><br></br>
<input type="button" value="Call Servlet" name="Call Servlet" id="call"/>
<div id="output"></div>
</body>
You are doing mistake in "configuration_page.jsp" file.
here in this file , function loadXMLDoc() 's line number 2 should be like this:
var config=document.getElementsByName('configselect').value;
because you have declared only the name attribute in your <select> tag. So you should get this element by name.
After correcting this, it will run without any JavaScript error
loadXMLDoc JS function should return false, otherwise it will result in postback.
I used an example script of how to load an SWF file with the JQuery SWF plugin (http://jquery.thewikies.com/swfobject/examples). I am trying to get the plugin to work in a JSP. It appears to work in FireFox and Chrome but not in IE8.
Can anyone see any obvious issues? Thanks in advance.
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
</head>
<script type="text/javascript" src="js/jquery.swfobject.1-1-1.js"></script>
<body>
<script type="text/javascript">
var bar_chart = $.flash.create (
{
swf: 'flash/open-flash-chart.swf',
width: 350,
height: 260,
wmode: 'transparent',
play: true,
flashvars: {
"get-data": "getChart1Data"
}
}
);
function getChart1Data()
{
return JSON.stringify(${chart1Data});
};
function ofc_ready()
{
/**/
};
$(document).ready(
function() {
$('#bar_chart').html(bar_chart);
}
);
</script>
<tr>
<td colspan="2">
<table>
<tr>
<td>
<div id="bar_chart"></div>
</td>
</tr>
</table>
</td>
</tr>
</body>
</html>
Your HTML is syntactically invalid. The browser behaviour is unpredictable.
This one is syntactically valid. Give it a try.
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert your title</title>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/jquery.swfobject.1-1-1.js"></script>
<script type="text/javascript">
var bar_chart = $.flash.create ({
swf: 'flash/open-flash-chart.swf',
width: 350,
height: 260,
wmode: 'transparent',
play: true,
flashvars: {
"get-data": "getChart1Data"
}
});
function getChart1Data() {
return JSON.stringify(${chart1Data});
}
function ofc_ready() {
/**/
}
$(document).ready(function() {
$('#bar_chart').html(bar_chart);
});
</script>
</head>
<body>
<div id="bar_chart"></div>
</body>
</html>
PS: I removed the table since it's incomplete and only adds noise to the demo.
All you have to do is change your swfobject version to latest SWFObject 2.2
<script type="text/javascript" src="js/jquery.swfobject.1-1-1.js"></script>
This will fix ie issue
try
flashMovie = null;
$(document).ready(
function () {
flashMovie = $('#bar_chart');
flashMovie.flash(
{
swf: 'flash/open-flash-chart.swf',
width: 350,
height: 260,
wmode: 'transparent',
play: true,
flashvars: { "get-data": "getChart1Data" }
}
);
}
);
function getChart1Data() { return JSON.stringify(${chart1Data}); };